【发布时间】:2019-09-16 05:51:01
【问题描述】:
我们仍在使用 Spring Boot 1.5.x,我们想开始使用 TestContainers。但是,所有示例都使用 Spring boot 2.x,它使用仅在 2.x 中可用的 TestPropertyValues 类。甚至可以在 1.5.x 中将新的属性值应用到可配置的上下文中吗?
这是在 2.x 中工作的代码:
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(initializers = {UserRepositoryTCIntegrationTest.Initializer.class})
public class UserRepositoryTCIntegrationTest extends UserRepositoryCommonIntegrationTests {
@ClassRule
public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1")
.withDatabaseName("integration-tests-db")
.withUsername("sa")
.withPassword("sa");
static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword()
).applyTo(configurableApplicationContext.getEnvironment());
}
}
}
【问题讨论】:
标签: spring-boot testcontainers