【发布时间】:2021-06-14 13:01:05
【问题描述】:
在我的 Spring Boot 2.4.3 应用程序中,我使用了 Testcontainers,并按照互联网上的说明进行操作。我有一个application.yaml:
spring:
datasource:
url: jdbc:tc:postgresql:13.2:///testdb?TC_INITSCRIPT=tc_initscript_postgresql.sql
username: duke
password: s3crEt
jpa:
database-platform: org.hibernate.dialect.PostgreSQL95Dialect
hibernate:
ddl-auto: create-drop
但是当我调试应用程序时,容器总是“测试”作为 URL、用户名和密码的值。
这是我的测试类:
@ActiveProfiles("test")
@Testcontainers
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class AbstractApplicationIT {
final static DockerImageName POSTGRES_IMAGE = DockerImageName.parse("postgres:13.2-alpine");
@Container
// public static GenericContainer postgreSQLContainer = new GenericContainer(POSTGRES_IMAGE)
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>(POSTGRES_IMAGE)
// .withInitScript("tc_initscript_postgresql.sql")
// .withPassword("password")
// .withUsername("username")
// .withDatabaseName("test")
// .withInitScript("tc_initscript_postgresql.sql")
;
// @DynamicPropertySource
// static void postgresqlProperties(DynamicPropertyRegistry registry) {
// registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
// registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
// registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
// }
@Test
public void contextLoads() {
System.out.println(postgreSQLContainer.getDatabaseName());
System.out.println(postgreSQLContainer.getUsername());
System.out.println(postgreSQLContainer.getPassword());
}
}
System.out:
测试
测试
测试
...即使不使用@DynamicPropertySource。
【问题讨论】:
-
我猜容器是在处理
application.properties文件之前启动的。
标签: spring-boot testcontainers application.properties