如何在Spring Boot中基于配置文件运行/关闭选择性测试
我有一个spring-boot
正在编写IT测试的应用程序。
测试数据来自application-dev.properties
我激活dev
配置文件时
这是我要进行测试的内容:
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ApplicationTests {
@Autowired
Environment env;
@Test
public void contextLoads() {
System.out.println(Arrays.toString((env.getActiveProfiles())));
}
}
服务IT测试
public class ServiceITTest extends ApplicationTests {
@value
String username;
@value
String address;
@Autowired
MyService myService;
@Test
public void check_for_valid_username_address(){
myService.validate(username,address);
}
}
我希望上述测试仅在设置“ dev”,“ qa”的配置文件时运行。默认情况下,它不应运行。
在Spring Boot测试中能否获得良好的控制?
-
您将要使用
@IfProfileValue
注释。不幸的是,它不能直接在活动配置文件上运行,而是可以读取属性,因此,如果您仅在要对其运行测试的配置文件中定义特定属性,则可以在该特定属性上使用该注释。http://docs.spring.io/spring/docs/current/spring-framework-
reference/html/integration-testing.html#integration-testing-annotations-
junit