ktv*_*ktv 3 postgresql integration-testing spring-boot testcontainers r2dbc
目前,我想为我的系统创建集成测试。我使用 testcontainers 生成临时数据库实例,并使用 R2DBC 数据库使我的系统具有反应性。问题是我不知道如何在 R2DBC testcontainer 实例中创建模式,testcontainers 网页中的R2DBC 支持和JDBC支持之间的文档存在明显差异。在 JDBC 中,在替换 JDBC URL 后有创建 schema 的部分,而在 R2DBC 中,在替换 R2DBC URL 后没有提及创建 schema。我已经尝试和探索了中的方法PostgreSQLR2DBCDatabaseContainer,但没有成功。
我还使用 spring boot 作为我们系统的框架,通常我使用初始值设定项替换 URL ContextConfiguration。替换 R2DBC 的 URL 后有什么方法可以创建架构吗?
您有以下选择来实现您想要的目标:
如果您使用Spring Boot,这里有一篇文章展示了它与单例模式的使用: https: //rieckpil.de/reuse-containers-with-testcontainers-for-fast-integration-tests/。
对于单例容器方法,您应该执行类似的操作:
public abstract class PostgresTestContainer {
private final PostgreSQLContainer<?> postgresContainer =
new PostgreSQLContainer<>("postgres:13.3-alpine")
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");
static {
postgresContainer.start();
}
@DynamicPropertySource
private static void setDatasourceProperties(DynamicPropertyRegistry registry) {
// JDBC DataSource Example
registry.add("spring.datasource.url", postgresContainer::getJdbcUrl);
registry.add("spring.datasource.password", postgresContainer::getPassword);
registry.add("spring.datasource.username", postgresContainer::getUsername);
// R2DBC DataSource Example
registry.add("spring.r2dbc.url", () ->
format("r2dbc:pool:postgresql://%s:%d/%s",
postgresContainer.getHost(),
postgresContainer.getFirstMappedPort(),
postgresContainer.getDatabaseName()));
registry.add("spring.r2dbc.username", postgresContainer::getUsername);
registry.add("spring.r2dbc.password", postgresContainer::getPassword);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5953 次 |
| 最近记录: |