加载测试专用属性
-
在启动测试环境时可以通过properties参数设置测试环境专用的属性。
// properties属性可以为当前测试用例添加临时属性,仅在这个测试类有效
@SpringBootTest (properties = {"test.prop=testValue1"})
public class PropertiesArgsTest {
@Value("${test.prop}")
private String msg;
@Test
void testProperties(){
System.out.println(msg);
}
}
-
在启动测试环境时可以通过args参数设置测试环境专用的传入参数。
// args属性可以为当前测试用例添加临时的命令行参数(源码级,可以保留下来)
@SpringBootTest (args = {"--test.prop=testValue2"})
public class PropertiesArgsTest {
@Value("${test.prop}")
private String msg;
@Test
void testProperties(){
System.out.println(msg);
}
}
-
总结:
1.加载测试临时属性应用于小范围测试环境。
2. yml,properties,args一起设置临时属性,args优先级高会覆盖他两个。
测试controller层启动web环境
模拟端口
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) //默认为NONE
public class WebTest {
@Test
void test(){
}
}
发送虚拟请求
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
// 虚拟调用模板
// 1. 开启虚拟mvc的调用
@AutoConfigureMockMvc
public class WebTest {
// 2. 注入虚拟mvc调用对象 方式: ①使用自动装配,②在形参上写
@Test
void testWeb(@Autowired MockMvc mvc) throws Exception {
// 3. 创建虚拟请求,当前访问/books
MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/books");
// 执行对应请求
mvc.perform(builder);
}
}
匹配响应执行状态
@Test
void 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);
}
}
匹配响应体
-
字符串格式
@Test
void 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格式
@Test
void 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);
}
匹配响应头
@Test
void 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);
}
-
项目测试总结:
@Test
void 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}