在SpringBoot中Controller层的单元测试该怎么作:java
package com.gy.demo.controller; 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 org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; /** * Description: API单元测试 * 注释: @AutoConfigureMockMvc 专门进行Controller层的测验 * @author geYang * @since 2017/12/28 **/ @SpringBootTest @AutoConfigureMockMvc @RunWith(SpringRunner.class) public class AaHelloControllerTest { @Autowired private MockMvc mockMvc; @Test public void helloTest() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("public/hello")) //判断验证返回状态码是否为500 .andExpect(MockMvcResultMatchers.status().isNotFound()); } }
参考大神: https://www.imooc.com/video/14341git
项目源码: https://gitee.com/ge.yang/SpringBootweb