【发布时间】:2015-11-06 15:30:16
【问题描述】:
我有这样的测试
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 注解的全部意义。
【问题讨论】: