• SpringBoot的测试


    添加测试属性

    优势:比多环境开发中的测试环境影响范围更小,仅对当前测试类有效。

    properties={"xxx"}与yml中的xxx一样。

     

    加载测试专用类

    加载测试范围配置应用于小范围测试环境。

    配置类

    1. @Configuration
    2. public class MsgConfig{
    3. @Bean
    4. public String msg(){
    5. return "hello world";
    6. }
    7. }

    测试类

    1. @SpringBootTest
    2. @Import({MsgConfig.class}) //加载指定类为bean,仅仅在该测试类中生效
    3. public class ConfigurationTest{
    4. @Autowired
    5. private String msg;
    6. @Test
    7. void testConfiguration(){
    8. System.out.println(msg); //说出hello world
    9. }
    10. }

    web环境模拟测试

    测试步骤

    1. 设置测试端口。

    2. 模拟测试启动。

    3. 模拟测试匹配(各组成部分信息均可匹配)。

    下面图片中的内容都可以匹配。

    基础代码

    1. //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
    2. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    3. @AutoConfigureMockMvc //开启web测试环境。
    4. class Xin1ApplicationTests {
    5. @Test
    6. void contextLoads(@Autowired MockMvc mvc) throws Exception {
    7. //创建虚拟,访问当前/book/query
    8. MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
    9. //执行请求
    10. ResultActions action = mvc.perform(builder);
    11. }
    12. }

    测试返回的标识是否成功

    1. //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
    2. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    3. @AutoConfigureMockMvc //开启web测试环境。
    4. class Xin1ApplicationTests {
    5. @Test
    6. void contextLoads(@Autowired MockMvc mvc) throws Exception {
    7. //创建虚拟,访问当前/book/query
    8. MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
    9. ResultActions action = mvc.perform(builder); //执行请求
    10. //匹配执行状态(是否预期值)
    11. //定义执行状态匹配器
    12. StatusResultMatchers status = MockMvcResultMatchers.status();
    13. //定义预期执行状态
    14. ResultMatcher ok = status.isOk();
    15. //使用本次真实执行结果与预期结果进行对比
    16. action.andExpect(ok);
    17. }
    18. }

    执行内容的匹配

    1. //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
    2. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    3. @AutoConfigureMockMvc //开启web测试环境。
    4. class Xin1ApplicationTests {
    5. @Test
    6. void contextLoads(@Autowired MockMvc mvc) throws Exception {
    7. //创建虚拟,访问当前/book/query
    8. MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
    9. ResultActions action = mvc.perform(builder); //执行请求
    10. //匹配执行状态(是否预期值)
    11. //定义执行状态匹配器
    12. ContentResultMatchers content = MockMvcResultMatchers.content();
    13. ResultMatcher t = content.string("hello test");
    14. //添加预计值到本次调用过程中进行匹配
    15. action.andExpect(t);
    16. }
    17. }

    虚拟请求体(JSON)匹配

    1. //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
    2. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    3. @AutoConfigureMockMvc //开启web测试环境。
    4. class Xin1ApplicationTests {
    5. @Test
    6. void contextLoads(@Autowired MockMvc mvc) throws Exception {
    7. //创建虚拟,访问当前/book/query
    8. MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
    9. ResultActions action = mvc.perform(builder); //执行请求
    10. //匹配执行状态(是否预期值)
    11. //定义执行状态匹配器
    12. ContentResultMatchers content = MockMvcResultMatchers.content();
    13. ResultMatcher t = content.json("{\"id\":66,\"type\":\"计算机\",\"name\":\"算法导论\",\"description\":\"很好\"}");
    14. //添加预计值到本次调用过程中进行匹配
    15. action.andExpect(t);
    16. }
    17. }

    示例

    1. //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
    2. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    3. @AutoConfigureMockMvc //开启web测试环境。
    4. class Xin1ApplicationTests {
    5. @Test
    6. void contextLoads(@Autowired MockMvc mvc) throws Exception {
    7. //创建虚拟,访问当前/book/query
    8. MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
    9. ResultActions action = mvc.perform(builder); //执行请求
    10. //返回的状态代码是正常的200
    11. StatusResultMatchers status = MockMvcResultMatchers.status();
    12. ResultMatcher ok = status.isOk();
    13. action.andExpect(ok);
    14. //返回的请求头是否与预期的结果一致
    15. HeaderResultMatchers header = MockMvcResultMatchers.header();
    16. ResultMatcher ContentType = header.string("Content-Type", "application/json");
    17. action.andExpect(ContentType);
    18. //返回的json是否与预期的结果一致
    19. ContentResultMatchers content = MockMvcResultMatchers.content();
    20. ResultMatcher t = content.json("{\"id\":66,\"type\":\"计算机\",\"name\":\"算法导论\",\"description\":\"很好\"}");
    21. action.andExpect(t);
    22. }
    23. }

    测试层事务回滚

    为测试用例添加事务:SPringBoot会对测试用例对应的事务提交操作进行回滚。

    1. @SpringBootTest
    2. @Transactional
    3. public class ServiceTest(){
    4. @Autowired
    5. private BookService bookService;
    6. @Test
    7. pbulic void testFun(){
    8. Book book = new Book(1,"算法导论");
    9. bookService.insert(book);
    10. }
    11. }

    如果想在测试用例中提交事务,可以通过@Rollback注解设置。

    一般@Transactional与@Rollback同时使用。

    1. @SpringBootTest
    2. @Transactional
    3. @Rollback(false) //true为回滚,false是不回滚
    4. public class Service{
    5. }

    测试用例设置随机数据

    测试用例数据通常采用随机值进行测试,使用SpringBoot提供的随机数为其赋值。

    基本使用

    1. testcase:
    2. book:
    3. id: ${random.int}
    4. name: ${random.value}
    5. uuid: ${random.uuid}
    6. publishTime: ${random.long}

    高级使用

    1. testcase:
    2. book:
    3. id: ${random.int(10)} # 生成10以内的数
    4. num: ${random.int(5,10)} # 生成510以内的数
    5. name: 李:${random.value}
    6. uuid: ${random.uuid}
    7. publishTime: ${random.long}

    实体类

    1. @Data
    2. @Component
    3. @ConfigurationProperties(prefix = "testcase.book")
    4. public class BookCase {
    5. private Integer id;
    6. private Integer num;
    7. private String name;
    8. private String uuid;
    9. private Long publishTime;
    10. }

  • 相关阅读:
    【每日十分钟前端】基础篇19,普通函数、箭头函数、构造函数的区别
    1785_GNU了解之一_主页上的简介
    【探索C++】C++对C语言的一些类的增强
    Flask入门学习教程
    【Spring Boot 源码研究 】- 自动化装配条件化配置Conditional剖析
    LeetCode 热题 HOT 100:二叉树专题
    全网最新版的超详细的xxl_job教程(2023年),以及解决“调度失败:执行器地址为空”的问题
    技术贴 | 深度解析 PostgreSQL Protocol v3.0(二)— 扩展查询
    将来会是Python、Java、Golang三足鼎立吗?
    Git详细介绍 -入门到实战万字篇(上)
  • 原文地址:https://blog.csdn.net/LYXlyxll/article/details/126671358