Loading... ## 前言: 一般我们进行项目接口开发都要进行接口测试,比如使用postman、apiFox工具,但是其实我们也可以直接在项目中进行接口测试,比如:请求地址是否正确,请求状态是否匹配,响应体,响应头等等都可以进行测试。 ## web环境模拟测试: ### 表现层: ```java @RestController @RequestMapping("/books") public class BookController { @GetMapping public String getById(){ System.out.println("getById is running ..."); return "springboot"; } @GetMapping("/getId") public Book getId(){ System.out.println("getId is running"); return new Book(1L, "springboot", "springboot", "springboot"); } } ``` ### 虚拟请求测试: ```java // 虚拟的web环境 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // 开启虚拟MVC调用 注意:一定要加下面这个注解 @AutoConfigureMockMvc public class WebTest { @Test void testWeb(@Autowired MockMvc mvc) throws Exception { // http://localhost:8080/books // 创建虚拟请求,当前访问/books MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books"); // 执行对应的请求 mvc.perform(builder); } } ``` ### 虚拟请求状态匹配: ```java @Test void testStatus(@Autowired MockMvc mvc) throws Exception { // http://localhost:8080/books // 创建虚拟请求,当前访问/books MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books"); // 执行对应的请求 final ResultActions actions = mvc.perform(builder); // 设置预期值 与真实值进行比较,成功测试通过,失败测试失败 // 定义本次调用的预期 final StatusResultMatchers status = MockMvcResultMatchers.status(); final ResultMatcher ok = status.isOk(); // 添加预计值到本次调用过程中进行匹配 actions.andExpect(ok); } ``` ### 虚拟请求响应体匹配: ```java @Test void testBody(@Autowired MockMvc mvc) throws Exception { // http://localhost:8080/books // 创建虚拟请求,当前访问/books MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books"); // 执行对应的请求 final ResultActions actions = mvc.perform(builder); // 设置预期值 与真实值进行比较,成功测试通过,失败测试失败 // 定义本次调用的预期 final ContentResultMatchers content = MockMvcResultMatchers.content(); final ResultMatcher result = content.string("springboot"); // 添加预计值到本次调用过程中进行匹配 actions.andExpect(result); } ``` ### 虚拟请求响应JSON匹配: ```java @Test void testJson(@Autowired MockMvc mvc) throws Exception { // http://localhost:8080/books // 创建虚拟请求,当前访问/books MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books/getId"); // 执行对应的请求 final ResultActions actions = mvc.perform(builder); // 设置预期值 与真实值进行比较,成功测试通过,失败测试失败 // 定义本次调用的预期 final ContentResultMatchers content = MockMvcResultMatchers.content(); final ResultMatcher result = content.json("{\"id\":1,\"name\":\"springboot\",\"type\":\"springboot\",\"description\":\"springboot\"}"); // 添加预计值到本次调用过程中进行匹配 actions.andExpect(result); } ``` ### 虚拟请求响应头匹配: ```java @Test public void testContentType(@Autowired MockMvc mvc) throws Exception { MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books"); ResultActions action = mvc.perform(builder); HeaderResultMatchers header = MockMvcResultMatchers.header(); ResultMatcher resultHeader = header.string("Content-Type", "application/json"); action.andExpect(resultHeader); } ``` ## 最后: 当我们的请求地址、请求状态、响应头、响应数据都成功匹配,这个接口很大概率不会出现问题,如果存在问题,大部分是业务逻辑或者与数据库的交互问题 最后修改:2022 年 08 月 10 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 1 如果文章有用,请随意打赏。