How to write a unit test for an endpoint of a Boot Controller Spring

I have a sample Spring Boot application with the following

Main loading class

@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } 

controller

 @RestController @EnableAutoConfiguration public class HelloWorld { @RequestMapping("/") String gethelloWorld() { return "Hello World!"; } } 

What is the easiest way to write unit test for a controller? I tried the following, but he complains that WebApplicationContext was not able to auto-increment

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = DemoApplication.class) public class DemoApplicationTests { final String BASE_URL = "http://localhost:8080/"; @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void testSayHelloWorld() throws Exception{ this.mockMvc.perform(get("/") .accept(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")); } @Test public void contextLoads() { } } 
+48
java spring-boot spring-test-mvc unit-testing junit
Mar 14 '15 at 20:43
source share
4 answers

Spring MVC offers standaloneSetup , which supports testing for relatively simple controllers without the need for context.

Create MockMvc by registering one or more instances of @Controller and configuring the Spring MVC infrastructure programmatically. This allows full control over the instantiation and initialization of controllers, and their dependencies, similar to simple block tests, and it is also possible to test one controller at a time.

An example test for your controller might be as simple as

 public class DemoApplicationTests { private MockMvc mockMvc; @Before public void setup() { this.mockMvc = standaloneSetup(new HelloWorld()).build(); } @Test public void testSayHelloWorld() throws Exception { this.mockMvc.perform(get("/").accept(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")); } } 
+36
Mar 14 '15 at 9:18
source share

The new testing enhancements that debuted in Spring Boot 1.4.M2 can help reduce the amount of code needed to write such a situation.

The test will look like this:

 import static org.springframework.test.web.servlet.request.MockMvcRequestB‌​uilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMat‌​chers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMat‌​chers.status; @RunWith(SpringRunner.class) @WebMvcTest(HelloWorld.class) public class UserVehicleControllerTests { @Autowired private MockMvc mockMvc; @Test public void testSayHelloWorld() throws Exception { this.mockMvc.perform(get("/").accept(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")); } } 

See this blog for more details, as well as documentation.

+22
Apr 20 '16 at 8:35
source share

Adding @WebAppConfiguration ( org.springframework.test.context.web.WebAppConfiguration ) annotations to your DemoApplicationTests class will work.

+4
Jan 21 '16 at 7:58
source share

Here is another answer using Spring MVC standaloneSetup. Using this method, you can either autwire the controller class or Mock it.

  import static org.mockito.Mockito.mock; import static org.springframework.test.web.server.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.server.result.MockMvcResultMatchers.content; import static org.springframework.test.web.server.result.MockMvcResultMatchers.status; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.server.MockMvc; import org.springframework.test.web.server.setup.MockMvcBuilders; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class DemoApplicationTests { final String BASE_URL = "http://localhost:8080/"; @Autowired private HelloWorld controllerToTest; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.standaloneSetup(controllerToTest).build(); } @Test public void testSayHelloWorld() throws Exception{ //Mocking Controller controllerToTest = mock(HelloWorld.class); this.mockMvc.perform(get("/") .accept(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(status().isOk()) .andExpect(content().mimeType(MediaType.APPLICATION_JSON)); } @Test public void contextLoads() { } } 
+4
Dec 12 '16 at 13:24
source share



All Articles