如何为Spring Boot Controller端点编写unit testing

我有一个以下样本Spring Boot应用程序

引导主类

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

调节器

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

为控制器编写unit testing最简单的方法是什么? 我尝试了以下,但它抱怨未能自动assemblyWebApplicationContext

 @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() { } } 

Spring MVC提供了一个独立的设置 ,支持testing相对简单的控制器,而不需要上下文。

通过注册一个或多个@ Controller实例并以编程方式configurationSpring MVC基础结构来构buildMockMvc。 这允许完全控制控制器的实例化和初始化,以及它们的依赖性,类似于普通的unit testing,同时也可以一次testing一个控制器。

你的控制器的一个例子testing可以像这样简单

 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")); } } 

在Spring Boot 1.4.M2中推出的新testing改进可以帮助您减less编写这种情况所需的代码量。

testing看起来像这样:

 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")); } } 

有关更多详细信息以及文档,请参阅此博客文章

@WebAppConfigurationorg.springframework.test.context.web.WebAppConfiguration )注释添加到您的DemoApplicationTests类将会起作用。

这里是使用Spring MVC的standaloneSetup的另一个答案。 使用这种方法,你可以自动assembly控制器类或者模拟它。

  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() { } } 
 @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")); } }