个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主;正在Java学习的路上摸爬滚打,记录学习的过程~ 个人主页:.29.的博客 学习社区:进去逛一逛~

加载测试范围的临时属性,应用于小范围测试环境
/**
* @author .29.
* @create 2023-04-01 20:28
*/
//properties属性,可以为当前测试用添加临时的属性配置
@SpringBootTest(properties = "test.prop=testValueByProperties")
public class PropertiesAndArgsTest {
@Value("${test.prop}")
private String msg;
@Test
public void testProperties(){
System.out.println(msg);
}
}
test:
prop: 优势:比多环境开发中的测试环境影响范围小,仅在当前测试类有效。
/**
* @author .29.
* @create 2023-04-01 20:28
*/
//args属性,可以为当前测试用例添加临时的命令行参数
@SpringBootTest(args = {"--test.prop=testValueByArgs"})
public class PropertiesAndArgsTest {
@Value("${test.prop}")
private String msg;
@Test
public void testProperties(){
System.out.println(msg);
}
}
test:
prop: 加载测试范围配置,应用于小范围测试环境
/**
* @author .29.
* @create 2023-04-01 21:27
*/
//专用的配置
@Configuration
public class MsgConfig {
@Bean
public String msg(){
return "test @Import get msg";
}
}/**
* @author .29.
* @create 2023-04-01 21:30
*/
@SpringBootTest
@Import(MsgConfig.class)
public class ConfigurationTest {
@Autowired
private String msg;
@Test
public void testConfiguration(){
System.out.println(msg);
}
}成功加载到专用配置中的内容:

Web环境模拟测试
webEnvironment属性 提供了启动Web环境的选择:
SpringBootTest.WebEnvironment.NONE:不启动Web服务器SpringBootTest.WebEnvironment.DEFINED_PORT:使用默认端口在测试类启动Web服务器/**
* @author .29.
* @create 2023-04-01 21:39
*/
//webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT:默认端口在测试类启动Web服务器
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class WebTest {
@Test
public void test(){
}
}
SpringBootTest.WebEnvironment.RANDOM_PORT:使用随机端口在测试类启动Web服务器/**
* @author .29.
* @create 2023-04-01 21:39
*/
//webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT: 随机端口 在测试类启动Web服务器
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebTest {
@Test
public void test(){
}
}
/**
* @author .29.
* @create 2023-04-01 22:38
*/
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping
public String get(){
System.out.println("get() is running ...");
return "Springboot";
}
}/**
* @author .29.
* @create 2023-04-01 21:39
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@AutoConfigureMockMvc注解:开启MVC虚拟调用
@AutoConfigureMockMvc
public class WebTest {
@Test //@Autowired注解,注入虚拟MVC调用对象
public void testWeb(@Autowired MockMvc mockMvc) throws Exception {
//创建虚拟请求,当前访问/books
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
//执行对应的请求
mockMvc.perform(builder);
}
}成功发起虚拟请求

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启MVC虚拟调用
@AutoConfigureMockMvc
public class WebTest {
@Test
public void testStatus(@Autowired MockMvc mockMvc) throws Exception {
//创建虚拟请求,当前访问/books (这里故意写错,模拟匹配失败)
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books1");
//执行对应的请求
ResultActions actions = mockMvc.perform(builder);
//设定预期值 与真实值进行比较,成功测试通过,失败测试失败
//定义本次调用的预期值
StatusResultMatchers status = MockMvcResultMatchers.status();
//预计本次调用是成功的:状态200
ResultMatcher ok = status.isOk();
//添加预期值到本次调用过程中,与真实执行结果进行匹配
actions.andExpect(ok);
}
}匹配失败时,输出匹配错误原因:

/**
* @author .29.
* @create 2023-04-01 21:39
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启MVC虚拟调用
@AutoConfigureMockMvc
public class WebTest {
@Test
public void testBody(@Autowired MockMvc mockMvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions actions = mockMvc.perform(builder);
//设定预期值 与真实值进行比较,成功测试通过,失败测试失败
//定义本次调用的预期值
ContentResultMatchers content = MockMvcResultMatchers.content();
//预计请求体为控制层组件的返回值"Springboot 测试类启动web环境 发送虚拟请求"
//如果请求体反馈JSON数据,string()改为json()
ResultMatcher body = content.string("Springboot");
//添加预期值到本次调用过程中,与真实执行结果进行匹配
actions.andExpect(body);
}
} @Test
public void testHeader(@Autowired MockMvc mockMvc) throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
ResultActions actions = mockMvc.perform(builder);
//设定预期值 与真实值进行比较,成功测试通过,失败测试失败
//定义本次调用的预期值
HeaderResultMatchers header = MockMvcResultMatchers.header();
//预计请求头的Content-Type 为 text/plain;charset=UTF-8
ResultMatcher string = header.string("Content-Type", "text/plain;charset=UTF-8");
//添加预期值到本次调用过程中,与真实执行结果进行匹配
actions.andExpect(string);
}
}