args级别高于properties
package com.tj.dao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
//properties属性可以为当前测试用例添加临时的属性设置
//@SpringBootTest(properties = {"test.prop=testValue1"})
//args属性可以为当前测试用例添加临时的属性设置
@SpringBootTest(args = {"--test.prop=testValue2"})
public class demoTest1 {
@Value("${test.prop}")
private String msg;
@Test
void testProperties(){
System.out.println(msg);
}
}
web环境模拟测试
模拟端口
//webEnvironment设置web服务环境测试
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
public class demoTest1 {
@Value("${test.prop}")
private String msg;
@Test //注入虚拟MVC调用对象
void testWeb(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求,当前访问/users
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users");
//执行请求
ResultActions action = mvc.perform(builder);
}
}