[英]Set Spring Unit Test Profile From -D Variables
我有這樣的測試
RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AppConfig.class, DevConfig.class, ProdConfig.class})
@ActiveProfiles({"prod", "dev"})
public class MyTest {
........
}
AppConfig是我的應用程序的主要配置
僅在我的單元測試中創建了兩個配置類,DEV配置類從我的src / test / resource /中加載test_dev.properties。
@Configuration
@Profile("dev")
@PropertySource("classpath:test_dev.properties")
public class DevConfig {
}
ProdConfig類加載prod.properties
@Configuration
@Profile("prod")
@PropertySource("classpath:test_prod.properties")
public class ProdConfig {
}
我想通過更改@ActiveProfiles中的值在prod和dev之間輕松切換
但是我希望能夠像這樣切換測試環境
mvn -Dspring.profiles.active=dev or prod install
我努力了
@ActiveProfiles({"prod", "dev"})
then
mvn -Dspring.profiles.active=dev or prod install
似乎產品總是被撿起
我看過像
@Test
public void transferTenDollars() throws InsufficientFundsException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev");
ctx.register(TransferServiceConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
ctx.refresh();
// proceed with assertions as above ...
}
但這將失去使用@ContextConfiguration批注的全部意義。
我認為開關是
-Dspring.profiles.active=dev
甚至作為環境變量
SPRING_PROFILES_ACTIVE=dev mvn install
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.