SpringBoot【实用篇】- 测试

文章目录

目标:

  • 加载测试专用属性
  • 加载测试专用配置
  • Web环境模拟测试
  • 数据层测试回滚
  • 测试用例数据设定
1.加载测试专用属性

我们在前面讲配置高级的时候是这样写的:

复制代码
test:
  prop: testValue

测试类:

复制代码
@SpringBootTest
public class PropertiesAndArgsTest {

    @Value("${test.prop}")
    private String msg;

    @Test
    void testProperties(){
        System.out.println(msg);
    }
}

那如果我把yml文件中的配置注释掉,我们还可以通过@SprinBootTest 来添加临时属性

复制代码
@SpringBootTest("test.prop = testValue")
public class PropertiesAndArgsTest {

    @Value("${test.prop}")
    private String msg;

    @Test
    void testProperties(){
        System.out.println(msg);
    }
}

如果两个都有谁生效呢? 答案是在这个测试类properties属性添加的临时属性配置中会覆盖yml的配置。

用args配也是可以的,使用args属性可以为当前测试用例添加临时的命令行参数

复制代码
//@SpringBootTest("test.prop = testValue")
@SpringBootTest(args = {"--test.prop=testValue2"})
public class PropertiesAndArgsTest {

    @Value("${test.prop}")
    private String msg;

    @Test
    void testProperties(){
        System.out.println(msg);
    }
}

那如果三个都有呢?

答案: 命令行级别参数(源码级别) > properties(idea)
小结:

加载测试临时属性应用小于小范围测试环境.

3.Web环境模拟测试

如果我们要想加入一个外部的bean来辅助我们测试:

复制代码
@Configuration
public class MsgConfig {

    @Bean
    public String msg(){
        return "bean msg";
    }
    
}

测试类中:

复制代码
@SpringBootTest
@Import(MsgConfig.class)
public class ConfigurationTest {

    @Autowired
    private String msg;

    @Test
    void testConfiguration() {
        System.out.println(msg);
    }

}


小结: 加载测试范围配置应用与小范围测试环境

能不能在测试样例中测试表现层呢?

2.加载测试专用配置
复制代码
@SpringBootTest
public class WebTest {

    @Test
    void test(){

    }
}

ctrl+左键 =》 查看SpringBootTest的源码,ctrl + f12查看方法

复制代码
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)

创建controller 然后模拟调用

复制代码
@RestController
@RequestMapping("/books")
public class BookController {

    @GetMapping
    public String getById(){
        System.out.println("getById is running...");
        return "SpringBoot";
    }
}



 @Test
    void testWeb(@Autowired MockMvc mvc) throws Exception {
        //http:localhost:8080/books
        //创建虚拟请求 当前访问路径为/books
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        mvc.perform(builder);

    }

ctrl + 左键 进去RequestBuilder查看源码,然后ctrl+h查看实现类


复制代码
   @Test
    void testStatus(@Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        ResultActions action = mvc.perform(builder);

        //设定预期值与真实值进行比较,成功测试通过,失败测试失败
        StatusResultMatchers status = MockMvcResultMatchers.status();
        //预计本次调用时成功的:状态200
        ResultMatcher ok = status.isOk();
        //添加预计值到本次调用过程中进行匹配
        action.andExpect(ok);

    }

如果你修改为book1


执行结果的匹配

复制代码
 @Test
    void testBody(@Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        ResultActions action = mvc.perform(builder);

        //设定预期值与真实值进行比较,成功测试通过,失败测试失败
        ContentResultMatchers content = MockMvcResultMatchers.content();
        ResultMatcher result = content.string("springboot");
        action.andExpect(result);

    }

但是我们以后是对json做匹配

复制代码
import lombok.Data;

@Data
public class Book {
    private int id;
    private String name;
    private String type;
    private String description;
}


 @GetMapping
    public Book getById(){
        System.out.println("getById is running...");
        Book book = new Book();
        book.setId(1);
        book.setName("SpringBoot");
        book.setType("Spring Framework");
        book.setDescription("This is a book about Spring Boot");
        return book;
    }
复制代码
 @Test
    void testJson(@Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        ResultActions action = mvc.perform(builder);

        //设定预期值与真实值进行比较,成功测试通过,失败测试失败
        ContentResultMatchers content = MockMvcResultMatchers.content();
        ResultMatcher result = content.json("{
" +
                "    "id": 1,
" +
                "    "name": "SpringBoot",
" +
                "    "type": "Spring Framework",
" +
                "    "description": "This is a book about Spring Boot"
" +
                "}");

        action.andExpect(result);

    }

我们也可以测试header-type 虚拟请求头匹配

复制代码
  @Test
    void testContentType(@Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
        ResultActions action = mvc.perform(builder);
        //设定预期值与真实值进行比较,成功测试通过,失败测试失败
        HeaderResultMatchers header = MockMvcResultMatchers.header();
        ResultMatcher contentType = header.string("Content-Type", "application/json;charset=UTF-8");
        action.andExpect(contentType);
    }
4.数据层测试回滚

有一种情况是:当我们测试业务层或者Dao层会留下结果数据,真实的企业开发会生成两个sql文件一个数据库的表创建的sql,一个数据库初始化的sql,但是当我们在开发的时候仍然需要测试仍然会留下数据,但是我们是想着我们测试只是想看看写的代码有没有问题,不需要留下数据,下面说的方法只服务于开发,上线后的另说。

我们可以用事务来进行回滚,

如何生成随机值来进行测试呢?

5.测试用例数据设定


封装实体类:


小结: 使用随机数据替换固定数据。

相关推荐
独断万古他化17 小时前
【SSM开发实战:博客系统】(三)核心业务功能开发与安全加密实现
spring boot·spring·mybatis·博客系统·加密
rannn_11117 小时前
【苍穹外卖|Day4】套餐页面开发(新增套餐、分页查询、删除套餐、修改套餐、起售停售)
java·spring boot·后端·学习
灵感菇_17 小时前
Java HashMap全面解析
java·开发语言
qq_124987075317 小时前
基于JavaWeb的大学生房屋租赁系统(源码+论文+部署+安装)
java·数据库·人工智能·spring boot·计算机视觉·毕业设计·计算机毕业设计
短剑重铸之日17 小时前
《设计模式》第十一篇:总结
java·后端·设计模式·总结
若鱼191917 小时前
SpringBoot4.0新特性-Observability让生产环境更易于观测
java·spring
倒流时光三十年17 小时前
SpringBoot 数据库同步 Elasticsearch 性能优化
数据库·spring boot·elasticsearch
觉醒大王17 小时前
强女思维:着急,是贪欲外显的相。
java·论文阅读·笔记·深度学习·学习·自然语言处理·学习方法
努力学编程呀(๑•ี_เ•ี๑)18 小时前
【在 IntelliJ IDEA 中切换项目 JDK 版本】
java·开发语言·intellij-idea
码农小卡拉18 小时前
深入解析Spring Boot文件加载顺序与加载方式
java·数据库·spring boot