Skip to content

Instantly share code, notes, and snippets.

@javaeeeee
Created February 25, 2017 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javaeeeee/eaf144cfa043615ffeb9194fe54c214e to your computer and use it in GitHub Desktop.
Save javaeeeee/eaf144cfa043615ffeb9194fe54c214e to your computer and use it in GitHub Desktop.
A Spring MVC REST controller test for a Spring Boot application
package com.javaeeeee.controllers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void getGreeting() throws Exception {
this.mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("Hello Spring World!"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment