• SpringBoot发送请求与匹配响应信息


    目录

    发送虚拟请求访问controller

    匹配响应执行状态

     匹配响应体

    匹配json格式响应体

    匹配响应头


    发送虚拟请求访问controller

    我们在test类中虚拟访问controller,就得发送虚拟请求。

    先创建一个controller

    1. package com.controller;
    2. import org.springframework.web.bind.annotation.GetMapping;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RestController;
    5. @RestController
    6. @RequestMapping("/tests")
    7. public class TestController {
    8. @GetMapping
    9. public String test(){
    10. System.out.println("test is running");
    11. return "test is success";
    12. }
    13. }

    在test中 ,这个是一个get请求,所以我们调用get,如果是put,则调用put即可

    1. package com;
    2. import org.junit.jupiter.api.Test;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. import org.springframework.test.web.servlet.MockMvc;
    7. import org.springframework.test.web.servlet.RequestBuilder;
    8. import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
    9. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    10. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    11. //开启虚拟MVC调用
    12. @AutoConfigureMockMvc
    13. public class WebTest {
    14. @Test
    15. // 注入虚拟MVC调用对象
    16. public void test(@Autowired MockMvc mvc) throws Exception {
    17. //创建虚拟请求,当前访问/tests,MockMvcRequestBuilders是一个工具类
    18. MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
    19. //执行请求
    20. mvc.perform(builder);
    21. }
    22. }

    访问需要用到的一个RequestBuilder,我们按ctrl+h显示出它的实现类 

     运行结果

     打印出了结果,说明访问成功

    匹配响应执行状态

    1. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    2. //开启虚拟MVC调用
    3. @AutoConfigureMockMvc
    4. public class WebTest {
    5. @Test
    6. // 注入虚拟MVC调用对象
    7. public void test(@Autowired MockMvc mvc) throws Exception {
    8. //创建虚拟请求,当前访问/tests,MockMvcRequestBuilders是一个工具类
    9. MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
    10. // 执行请求
    11. ResultActions action = mvc.perform(builder);
    12. //设置预期值与真实值进行比较,成功则测试通过,失败则测试不通过
    13. //定义本次调用的预期值
    14. StatusResultMatchers status= MockMvcResultMatchers.status();
    15. //预计本次调用成功的状态为200
    16. ResultMatcher ok=status.isOk();
    17. //添加预计值到本次调用过程中进行匹配
    18. action.andExpect(ok);
    19. }
    20. }

    运行成功不会有任何反应

     当将get改为put制造一个错误,或修改不存在的路径等其他错误,则就会报出错误信息。

     匹配响应体

    虚拟请求体匹配

    1. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    2. //开启虚拟MVC调用
    3. @AutoConfigureMockMvc
    4. public class WebTest {
    5. @Test
    6. // 注入虚拟MVC调用对象
    7. public void testBody(@Autowired MockMvc mvc) throws Exception {
    8. //创建虚拟请求,当前访问/tests,MockMvcRequestBuilders是一个工具类
    9. MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
    10. // 执行请求
    11. ResultActions action = mvc.perform(builder);
    12. //设置预期值与真实值进行比较,成功则测试通过,失败则测试不通过
    13. //定义本次调用的预期值
    14. ContentResultMatchers content = MockMvcResultMatchers.content();
    15. //预计本次调用成功的状态为200
    16. ResultMatcher result= content.string("test is success1");
    17. //添加预计值到本次调用过程中进行匹配
    18. action.andExpect(result);
    19. }
    20. }

    如果一致则不会有任何错误信息出现, 若信息不一致,则会出现

    匹配json格式响应体

    先创建一个类pojo对象

    1. package com.pojo;
    2. import lombok.Data;
    3. @Data
    4. public class Person {
    5. private String name;
    6. private String age;
    7. private String detail;
    8. }

    controller下 

    1. package com.controller;
    2. import com.pojo.Person;
    3. import org.springframework.web.bind.annotation.PutMapping;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. @RequestMapping("/tests")
    8. public class TestController {
    9. @RequestMapping("/person")
    10. public Person testPerson(){
    11. Person person = new Person();
    12. person.setName("zhangsan");
    13. person.setAge("14");
    14. person.setDetail("xijie");
    15. return person;
    16. }
    17. }

    启动访问得到一组json数据 

     我们在测试类中修改一个,使他产生错误的信息

    1. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    2. //开启虚拟MVC调用
    3. @AutoConfigureMockMvc
    4. public class WebTest
    5. @Test
    6. // 注入虚拟MVC调用对象
    7. public void testJson(@Autowired MockMvc mvc) throws Exception {
    8. //创建虚拟请求,当前访问/tests,MockMvcRequestBuilders是一个工具类
    9. MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests/person");
    10. // 执行请求
    11. ResultActions action = mvc.perform(builder);
    12. ContentResultMatchers content = MockMvcResultMatchers.content();
    13. ResultMatcher result= content.json("{\"name\":\"zhangsan\",\"age\":\"14\",\"detail\":\"xijie1\"}");
    14. //添加预计值到本次调用过程中进行匹配
    15. action.andExpect(result);
    16. }
    17. }

    运行结果 

    匹配响应头

    1. @Test
    2. // 注入虚拟MVC调用对象
    3. public void testHeader(@Autowired MockMvc mvc) throws Exception {
    4. MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
    5. ResultActions action = mvc.perform(builder);
    6. HeaderResultMatchers header = MockMvcResultMatchers.header();
    7. ResultMatcher result = header.string("Content-Type", "application/json");
    8. //添加预计值到本次调用过程中进行匹配
    9. action.andExpect(result);
    10. }

    匹配了一个/tests,返回字符串的方法。,就可以看出它的差别了 

    1. @RestController
    2. @RequestMapping("/tests")
    3. public class TestController {
    4. @GetMapping
    5. public String test(){
    6. System.out.println("test is running");
    7. return "test is success";
    8. }
    9. }

     一般的做法都是将这些写在同一方法。

  • 相关阅读:
    Django AssertionError: .accepted_renderer not set on Response
    C语言 位操作
    Qt之实现圆形进度条
    智慧党务管理源码,竞赛答题+阅读学习一套系统全搞定
    软设上午题错题知识点4
    Swagger问题记录
    ZooKeeper面试题
    算法学习笔记(3.1): ST算法
    u-boot 通过 SD 卡启动 Linux(三)
    2024北京护眼产品展/北京眼视光展/北京叶黄素展/中国眼博会
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/127388893