测试"/>
springboot启动测试
加载测试专用属性
-
在启动测试环境时可以通过properties参数设置测试环境专用的属性。
// properties属性可以为当前测试用例添加临时属性,仅在这个测试类有效
@SpringBootTest (properties = {"test.prop=testValue1"})
public class PropertiesArgsTest {@Value("${test.prop}")private String msg;@Testvoid testProperties(){System.out.println(msg);}
}
-
在启动测试环境时可以通过args参数设置测试环境专用的传入参数。
// args属性可以为当前测试用例添加临时的命令行参数(源码级,可以保留下来)
@SpringBootTest (args = {"--test.prop=testValue2"})
public class PropertiesArgsTest {@Value("${test.prop}")private String msg;@Testvoid testProperties(){System.out.println(msg);}
}
-
总结:
1.加载测试临时属性应用于小范围测试环境。
2. yml,properties,args一起设置临时属性,args优先级高会覆盖他两个。
测试controller层启动web环境
模拟端口
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) //默认为NONE
public class WebTest {@Testvoid test(){}
}
发送虚拟请求
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
// 虚拟调用模板
// 1. 开启虚拟mvc的调用
@AutoConfigureMockMvc
public class WebTest {
// 2. 注入虚拟mvc调用对象 方式: ①使用自动装配,②在形参上写
@Testvoid testWeb(@Autowired MockMvc mvc) throws Exception {
// 3. 创建虚拟请求,当前访问/booksMockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/books");
// 执行对应请求mvc.perform(builder);
}
}
匹配响应执行状态
@Testvoid testStatus(@Autowired MockMvc mvc) throws Exception {MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/books");ResultActions action = mvc.perform(builder);
// 设定预期值,与真实值进行比较,成功测试通过,否则失败
// status定义本次调用的预期值 ,(看需求选择)StatusResultMatchers status = MockMvcResultMatchers.status();
// 预计本次调用时成功的;状态200 (看需求选择)ResultMatcher ok = status.isOk();
// 添加预计值到本次调用过程中进行匹配action.andExpect(ok);}
}
匹配响应体
-
字符串格式
@Testvoid testBody(@Autowired MockMvc mvc) throws Exception {MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/book");ResultActions action = mvc.perform(builder);
// 设定预期值,与真实值进行比较,成功测试通过,否则失败
// 定义本次调用的预期值 ,(看需求选择)ContentResultMatchers content = MockMvcResultMatchers.content();ResultMatcher result = content.string("springbook running");//把预计的内容转换为字符串
// 添加预计值到本次调用过程中进行匹配action.andExpect(result);}
-
json格式
@Testvoid testJson(@Autowired MockMvc mvc) throws Exception {MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/book");ResultActions action = mvc.perform(builder);
// 设定预期值,与真实值进行比较,成功测试通过,否则失败ContentResultMatchers content = MockMvcResultMatchers.content();ResultMatcher result = content.json("{\"id\":1,\"name\":\"springname\",\"type\":\"springname\",\"description\":\"springname\"}");
// 添加预计值到本次调用过程中进行匹配action.andExpect(result);}
匹配响应头
@Testvoid testContentType(@Autowired MockMvc mvc) throws Exception {MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/book");ResultActions action = mvc.perform(builder);
// 设定预期值,与真实值进行比较,成功测试通过,否则失败HeaderResultMatchers header = MockMvcResultMatchers.header(); // (按测试需求写)ResultMatcher contentType = header.string("Content-Type", "application/json");//名称不要自己写,运行复制
// 添加预计值到本次调用过程中进行匹配action.andExpect(contentType);}
-
项目测试总结:
@Testvoid testGetById(@Autowired MockMvc mvc) throws Exception {
// 发送请求MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/book");ResultActions action = mvc.perform(builder);//定义规则,进行匹配StatusResultMatchers status = MockMvcResultMatchers.status();ResultMatcher ok = status.isOk();action.andExpect(ok);//定义规则,进行匹配HeaderResultMatchers header = MockMvcResultMatchers.header(); // (按测试需求写)ResultMatcher contentType = header.string("Content-Type", "application/json");//不要自己写action.andExpect(contentType);//定义规则,进行匹配ContentResultMatchers content = MockMvcResultMatchers.content();ResultMatcher result = content.json("{\"id\":1,\"name\":\"springname\",\"type\":\"springname\",\"description\":\"springname\"}");action.andExpect(result);}
业务层测试事务回滚
-
问题
一旦执行了某个生命周期,包含了测试过程,会给数据库留下数据,也就是运行测试一次留一组数据。
-
解决方案:
1. 在测试类中添加@Transactional是spring添加事务的注解。
2. 在测试类中添加@Rollback()注解是是否回滚,默认为true可以不用写注解,false不回滚。
设置随机数据
testcase:book:id: ${random.int}id2: ${random.int(10)} #生成10以内name: ${random.value}uuid: ${random.uuid}publishTime: ${random.long}
更多推荐
springboot启动测试
发布评论