Spring boot Testcontainers和Spring Boot 1.5
我们仍然在使用SpringBoot1.5.x,我们希望开始使用TestContainers。但是,所有示例都是SpringBoot2.x,它使用的Spring boot Testcontainers和Spring Boot 1.5,spring-boot,testcontainers,Spring Boot,Testcontainers,我们仍然在使用SpringBoot1.5.x,我们希望开始使用TestContainers。但是,所有示例都是SpringBoot2.x,它使用的TestPropertyValue类仅在2.x中可用。甚至可以在1.5.x中对可配置上下文应用新的属性值吗 这是在2.x中工作的代码: @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(initializers = {UserRepositoryTCIntegratio
TestPropertyValue
类仅在2.x中可用。甚至可以在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());
}
}
@RunWith(SpringRunner.class)
@春靴测试
@ContextConfiguration(初始值设定项={UserRepositoryTCIntegrationTest.Initializer.class})
公共类UserRepositoryTCIntegrationTest扩展了UserRepositoryCommonIntegrationTests{
@阶级规则
公共静态PostgreSQLContainer PostgreSQLContainer=新的PostgreSQLContainer(“postgres:11.1”)
.withDatabaseName(“集成测试数据库”)
.withUsername(“sa”)
.使用密码(“sa”);
静态类初始值设定项
实现ApplicationContextInitializer{
公共无效初始化(ConfigurableApplicationContext ConfigurableApplicationContext){
TestPropertyValue.of(
“spring.datasource.url=“+postgreSQLContainer.getJdbcUrl(),
“spring.datasource.username=“+postgreSQLContainer.getUsername(),
“spring.datasource.password=“+postgreSQLContainer.getPassword()
).applyTo(configurableApplicationContext.getEnvironment());
}
}
}好问题:)。您可以使用SpringBoot1.5+TestContainers设置testcontext。您可以使用以下选项,而不是通过使用动态值设置数据源属性(如示例代码中所示)来使用间接方式:
通过@TestConfiguration提供数据源Bean
好问题:)。您可以使用SpringBoot1.5+TestContainers设置testcontext。您可以使用以下选项,而不是通过使用动态值设置数据源属性(如示例代码中所示)来使用间接方式:
通过@TestConfiguration提供数据源Bean
只需使用以下命令即可启动数据库容器: 应用程序属性
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.url=jdbc:tc:postgresql:11://localhost/test
注:
Testcontainers需要在运行时位于应用程序的类路径上,才能正常工作只需使用以下命令即可启动数据库容器: 应用程序属性
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.url=jdbc:tc:postgresql:11://localhost/test
注:
Testcontainers需要在运行时位于应用程序的类路径上,才能正常工作