Java 是否可以动态设置SpringBootTest属性?
我有一个SpringBoot应用程序,我正试图在它的帮助下进行测试。我有点像:Java 是否可以动态设置SpringBootTest属性?,java,spring-boot,testcontainers,Java,Spring Boot,Testcontainers,我有一个SpringBoot应用程序,我正试图在它的帮助下进行测试。我有点像: @SpringBootTest public class DummyIT { @ClassRule public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer(); static { postgreSQLContainer.start(); String ur
@SpringBootTest
public class DummyIT {
@ClassRule
public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();
static {
postgreSQLContainer.start();
String url = postgreSQLContainer.getJdbcUrl();
}
}
不幸的是,TestContainer使用随机端口,如果我理解正确,我们不应该使用任何其他方式,这意味着postgreSQLContainer.getJdbcUrl()
的结果是不确定的
此外,我的应用程序从其application.properties
中检索数据库url,我的目标是在运行时首次使用之前,从postgreSQLContainer.getJdbcUrl()
提供的值中替换该值。有可能做到这一点吗
谢谢您的帮助。您可以使用
比如说,
@SpringBootTest(
properties = {
"spring.datasource.url=jdbc:tc:postgresql:9.6.8://localhost/your-database",
"spring.datasource.username=dummy-user",
"spring.datasource.password=dummy-password",
"spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect",
"spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver"
})
public class DummyIT {
}
Testcontainers驱动程序
ContainerDatabaseDriver
将创建并启动一个名为数据库的PostgreSQL server 9.6.8 anwser在这里被描述为:谢谢你,我周一第一件事就是试试它!