SpringBoot测试
@RunWith(SpringRunner.class) @SpringBootTest public class UserControllerTest { //用于伪造webMVC环境,模拟tomcat启动后的web程序 @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup(){ mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } //perform表示请求 //MockMvcRequestBuilders.get("/users")模拟get请求 //MockMvcRequestBuilders.get("/users") 请求格式 //andExpect表示服务请求的期望结果 // MockMvcResultMatchers.status().isOk(),http返回的状态码 //MockMvcResultMatchers.jsonPath("$.length()").value(3) //返回的集合数量(期待返回是一个集合 集合长度是3) @Test public void whenQuerySuccess() throws Exception{ mockMvc.perform(MockMvcRequestBuilders.get("/users") .contentType(MediaType.APPLICATION_JSON_UTF8) ).andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3)); } }
关于jsonPath说明:https://github.com/json-path/JsonPath
原文:https://www.cnblogs.com/rookieMemory/p/11369844.html