1.web环境模拟测试
设置测试端口(见SpringBoot启用web模拟测试(一))
模拟测试启动(见SpringBoot启用web模拟测试(一))
测试模拟匹配(各组成信息皆可配置)
@Test public void testAll() throws Exception { MockHttpServletRequestBuilder mockHttpServletRequestBuilder= MockMvcRequestBuilders.get("/books/get/4"); ResultActions resultActions = mvc.perform(mockHttpServletRequestBuilder); HeaderResultMatchers header = MockMvcResultMatchers.header(); ResultMatcher con = header.string("Content-Type", "application/json"); resultActions.andExpect(con); ContentResultMatchers content = MockMvcResultMatchers.content(); ResultMatcher json = content.contentType("application/json"); resultActions.andExpect(json); ContentResultMatchers content1 = MockMvcResultMatchers.content(); ResultMatcher json1 = content1.contentType("application/json"); resultActions.andExpect(json1); ContentResultMatchers content2 = MockMvcResultMatchers.content(); //预计本次调用时成功的状态200 ResultMatcher string = content2.string("{\"flag\":true,\"data\":{\"id\":null,\"type\":null,\"name\":\"333333\",\"description\":null},\"mess\":null}"); //添加预计值到本次调用中进行匹配 resultActions.andExpect(string); }