优势:比多环境开发中的测试环境影响范围更小,仅对当前测试类有效。
properties={"xxx"}与yml中的xxx一样。
加载测试范围配置应用于小范围测试环境。
配置类
- @Configuration
- public class MsgConfig{
- @Bean
- public String msg(){
- return "hello world";
- }
- }
测试类
- @SpringBootTest
- @Import({MsgConfig.class}) //加载指定类为bean,仅仅在该测试类中生效
- public class ConfigurationTest{
- @Autowired
- private String msg;
-
- @Test
- void testConfiguration(){
- System.out.println(msg); //说出hello world
- }
-
- }
设置测试端口。
模拟测试启动。
下面图片中的内容都可以匹配。
- //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
- @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
- @AutoConfigureMockMvc //开启web测试环境。
- class Xin1ApplicationTests {
-
- @Test
- void contextLoads(@Autowired MockMvc mvc) throws Exception {
- //创建虚拟,访问当前/book/query
- MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
-
- //执行请求
- ResultActions action = mvc.perform(builder);
- }
- }
- //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
- @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
- @AutoConfigureMockMvc //开启web测试环境。
- class Xin1ApplicationTests {
-
- @Test
- void contextLoads(@Autowired MockMvc mvc) throws Exception {
- //创建虚拟,访问当前/book/query
- MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
- ResultActions action = mvc.perform(builder); //执行请求
- //匹配执行状态(是否预期值)
- //定义执行状态匹配器
- StatusResultMatchers status = MockMvcResultMatchers.status();
- //定义预期执行状态
- ResultMatcher ok = status.isOk();
- //使用本次真实执行结果与预期结果进行对比
- action.andExpect(ok);
- }
- }
- //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
- @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
- @AutoConfigureMockMvc //开启web测试环境。
- class Xin1ApplicationTests {
-
- @Test
- void contextLoads(@Autowired MockMvc mvc) throws Exception {
- //创建虚拟,访问当前/book/query
- MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
- ResultActions action = mvc.perform(builder); //执行请求
- //匹配执行状态(是否预期值)
- //定义执行状态匹配器
- ContentResultMatchers content = MockMvcResultMatchers.content();
- ResultMatcher t = content.string("hello test");
- //添加预计值到本次调用过程中进行匹配
- action.andExpect(t);
- }
- }
- //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
- @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
- @AutoConfigureMockMvc //开启web测试环境。
- class Xin1ApplicationTests {
-
- @Test
- void contextLoads(@Autowired MockMvc mvc) throws Exception {
- //创建虚拟,访问当前/book/query
- MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
- ResultActions action = mvc.perform(builder); //执行请求
- //匹配执行状态(是否预期值)
- //定义执行状态匹配器
- ContentResultMatchers content = MockMvcResultMatchers.content();
- ResultMatcher t = content.json("{\"id\":66,\"type\":\"计算机\",\"name\":\"算法导论\",\"description\":\"很好\"}");
- //添加预计值到本次调用过程中进行匹配
- action.andExpect(t);
- }
- }
- //SpringBootTest.WebEnvironment.RANDOM_PORT为随机端口
- @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
- @AutoConfigureMockMvc //开启web测试环境。
- class Xin1ApplicationTests {
-
- @Test
- void contextLoads(@Autowired MockMvc mvc) throws Exception {
- //创建虚拟,访问当前/book/query
- MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/book/query");
- ResultActions action = mvc.perform(builder); //执行请求
-
- //返回的状态代码是正常的200
- 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);
-
- //返回的json是否与预期的结果一致
- ContentResultMatchers content = MockMvcResultMatchers.content();
- ResultMatcher t = content.json("{\"id\":66,\"type\":\"计算机\",\"name\":\"算法导论\",\"description\":\"很好\"}");
- action.andExpect(t);
- }
- }
为测试用例添加事务:SPringBoot会对测试用例对应的事务提交操作进行回滚。
- @SpringBootTest
- @Transactional
- public class ServiceTest(){
- @Autowired
- private BookService bookService;
-
- @Test
- pbulic void testFun(){
- Book book = new Book(1,"算法导论");
- bookService.insert(book);
- }
- }
如果想在测试用例中提交事务,可以通过@Rollback注解设置。
一般@Transactional与@Rollback同时使用。
- @SpringBootTest
- @Transactional
- @Rollback(false) //true为回滚,false是不回滚
- public class Service{
- }
测试用例数据通常采用随机值进行测试,使用SpringBoot提供的随机数为其赋值。
基本使用
- testcase:
- book:
- id: ${random.int}
- name: ${random.value}
- uuid: ${random.uuid}
- publishTime: ${random.long}
高级使用
- testcase:
- book:
- id: ${random.int(10)} # 生成10以内的数
- num: ${random.int(5,10)} # 生成5到10以内的数
- name: 李:${random.value}
- uuid: ${random.uuid}
- publishTime: ${random.long}
实体类
- @Data
- @Component
- @ConfigurationProperties(prefix = "testcase.book")
- public class BookCase {
- private Integer id;
- private Integer num;
- private String name;
- private String uuid;
- private Long publishTime;
- }