spring test是否设置了一些环境属性,我可以利用@ConditionalOnProperty
注释有条件地选择Spring启动自动配置工厂的两个实现?
答案 0 :(得分:0)
好的,所以Spring测试驱动程序可能会设置一个属性。但是,我找到了一种通过Spring Profile支持实现我的用例的简洁方法。
下面的大纲是针对testng
框架的。我概述了DataSource bean的条件连接 - 对于Integration,我创建了一个嵌入式mysql实例。 (将来由集成测试托管的docker mysql实例替换。)
步骤1:所有集成测试应启用集成配置文件:
// Create a TestNG Base class for integration testing.
@Test(groups = "Integration")
@ActiveProfiles("Integration")
public abstract class PayliquidAbstractTestNGIntegrationTest extends AbstractTestNGSpringContextTests {
}
// Use the base class as follows, Leave the @SpringApplicationConfiguration on The test classes as each starter module should confine the create application context to it's configuration hierarchy.
@SpringApplicationConfiguration(TestApplication.class)
public class SomeDaoIntegrationTest extends PayliquidAbstractTestNGIntegrationTest {
//...
}
步骤2:根据配置文件,可以有条件地连接Bean:
@Bean(name = "primaryDataSource")
@Primary
@Profile("Integration")
public DataSource primaryIntegrationDatasource() {
return JdbcClients.getIntegrationDatasource();
}
步骤3:也可以根据个人资料排除豆类:
@Bean
@Primary
@Profile("!Integration")
public DataSource primaryDataSource() {
HikariDataSource hks = new HikariDataSource(datasourceConfig);
logger.info("HikariCP MinimumIdle={}", hks.getMinimumIdle());
logger.info("HikariCP MaxPoolSize={}", hks.getMaximumPoolSize());
return hks;
}