> 文章列表 > 测试---

测试---

测试---

1.加载测试专用属性

1.在启动测试环境时可以通过properties参数设置测试环境专用的属性

@SpringBootTest(properties = {"test.value=kllda"})
public class PropertiesTest {@Value("${test.value}")private String msg;
​@Testpublic void testPro(){System.out.println(msg);}
​
}

优势:比多环境开发中的测试环境影响范围更小,仅当前测试类有效

2.在启动测试环境时可以通过args参数设置测试环境专用的传入参数

@SpringBootTest(args = "--test.value=--test")
public class PropertiesTest {@Value("${test.value}")private String msg;
​@Testpublic void testPro(){System.out.println(msg);}
}

2.加载测试专用配置

使用@Import注解加载当前测试类专用的配置

@Configuration//定义这个配置类交给Spring容器管理
public class MsgTest {@Beanpublic String configTest(){return "hello";}
}
@SpringBootTest
@Import(MsgTest.class)
public class ConfigT {@Autowiredprivate String msg;@Testpublic void getMsg(){System.out.println(msg);}
}

3.Web环境模拟测试

1.模拟端口

//端口随机 RANDOM_PORT
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebTest {@Testpublic void randomTest(){
​}

2.虚拟请求测试

 

//端口随机
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
​
//开启虚拟请求
@AutoConfigureMockMvc
​
public class WebTest {
@Testpublic void mvcv(@Autowired MockMvc mvc) throws Exception {//创建虚拟请求访问当前的"/books"MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/books");//执行请求ResultActions actions = mvc.perform(builder);
​}
}

3.虚拟请求状态匹配

//发出request请求,看状态码和预期的是否相同
@Test
public void testStatus(@Autowired MockMvc mvc) throws Exception {//创建虚拟请求访问当前的"/books"MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/books");//执行请求ResultActions actions = mvc.perform(builder);//设定预期值与真实之间的比较,成功测试通过,失败测试失败//定义本次调用的预期值StatusResultMatchers status = MockMvcResultMatchers.status();//预计本次调用成功的状态200ResultMatcher ok=status.isOk();//添加预计值到本次调用过程中进行匹配actions.andExpect(ok);
}

4.虚拟请求响应体匹配

//查看请求体的数据与预期的是否相同
@Test
public void testContent(@Autowired MockMvc mvc) throws Exception {//创建虚拟请求访问当前的"/books"MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/books");//执行请求ResultActions actions = mvc.perform(builder);//设定预期值与真实之间的比较,成功测试通过,失败测试失败//定义本次调用的预期值ContentResultMatchers content = MockMvcResultMatchers.content();//预期请求体的内容ResultMatcher resultMatcher = content.string("hello");//添加预计值到本次调用过程中进行匹配actions.andExpect(resultMatcher);
}

5.虚拟请求响应体(Json)匹配

//json格式的数据
@Test
public void testJson(@Autowired MockMvc mvc) throws Exception {//创建虚拟请求访问当前的"/books"MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/books");//执行请求ResultActions actions = mvc.perform(builder);//设定预期值与真实之间的比较,成功测试通过,失败测试失败//定义本次调用的预期值ContentResultMatchers content = MockMvcResultMatchers.content();//预期请求体的内容ResultMatcher resultMatcher = content.string("{\\"id\\":2,\\"name\\":\\"111\\",\\"type\\":\\"111\\",\\"description\\":\\"111\\"}");//添加预计值到本次调用过程中进行匹配actions.andExpect(resultMatcher);
}

6.虚拟请求响应头匹配

@Test
public void testContentType(@Autowired MockMvc mvc) throws Exception {//创建虚拟请求访问当前的"/books"MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/books");//执行请求ResultActions actions = mvc.perform(builder);//设定预期值与真实之间的比较,成功测试通过,失败测试失败//定义本次调用的预期值HeaderResultMatchers headerResultMatchers = MockMvcResultMatchers.header();//预期请求体的内容.ResultMatcher contentType = headerResultMatchers.string("Content-Type", "application/json");actions.andExpect(contentType);
}

4.数据测试回滚

为测试用例添加事务,SpringBoot会对测试用例对应的事务提交操作进行回滚

@SpringBootTest
@Transactional//在测试时不会让数据库留下残留数据
public class BookServiceTest {@Autowiredprivate BookService bookService;@Testpublic void testServcie(){Book book=new Book();book.setType("fsfs");book.setDescription("adsada");book.setName("ssssd");boolean flag = bookService.save(book);System.out.println(flag);}
}

如果想在测试用例中提交事务,可以通过@Rollback注解设置

@SpringBootTest
@Transactional//在测试时不会让数据库留下垃圾数据
@Rollback(true)//会出现回滚
public class BookServiceTest {@Autowiredprivate BookService bookService;@Testpublic void testServcie(){Book book=new Book();book.setType("fsfs");book.setDescription("adsada");book.setName("ssssd");boolean flag = bookService.save(book);System.out.println(flag);}
}

5.测试用例数据设定

测试用例数据通常采用随机值进行测试,使用SpringBoot提供的随机数为其赋值

 

${random.int}表示随机整数 ${random.int(10)}表示10以内的随机数 ${random.int(10,20)}表示10到20的随机数 其中()可以是任意字符,例如[],!!均可