SpringBoot【实用篇】- 测试

📅 发布时间:2026/7/8 5:35:14 👁️ 浏览次数:
SpringBoot【实用篇】- 测试
文章目录目标1.加载测试专用属性3.Web环境模拟测试2.加载测试专用配置4.数据层测试回滚5.测试用例数据设定目标加载测试专用属性加载测试专用配置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.proptestValue2}) public class PropertiesAndArgsTest { Value(${test.prop}) private String msg; Test void testProperties(){ System.out.println(msg); } }那如果三个都有呢答案 命令行级别参数(源码级别) propertiesidea小结加载测试临时属性应用小于小范围测试环境.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查看源码然后ctrlh查看实现类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;charsetUTF-8); action.andExpect(contentType); }4.数据层测试回滚有一种情况是当我们测试业务层或者Dao层会留下结果数据真实的企业开发会生成两个sql文件一个数据库的表创建的sql一个数据库初始化的sql但是当我们在开发的时候仍然需要测试仍然会留下数据但是我们是想着我们测试只是想看看写的代码有没有问题不需要留下数据下面说的方法只服务于开发上线后的另说。我们可以用事务来进行回滚,如何生成随机值来进行测试呢5.测试用例数据设定封装实体类小结: 使用随机数据替换固定数据。