Spring testcontainers Driver org.testcontainers.jdbc.ContainerDatabaseDriver 声称不接受 jdbcUrl

标签 spring spring-boot integration-testing testcontainers

对我的集成测试进行了以下配置后,我遇到了以下异常:
Driver org.testcontainers.jdbc.ContainerDatabaseDriver claims to not accept jdbcUrl, jdbc:postgresql://localhost:32864/test?loggerLevel=OFF

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WebApplication.class)
@AutoConfigureMockMvc
@Testcontainers
@TestPropertySource(ResourceUtils.CLASSPATH_URL_PREFIX + "application-test.properties")
public abstract class AbstractIntegrationTest {

    @Autowired
    protected MockMvc mockMvc;

    @Container
    protected static PostgreSQLContainer<?> postgresqlContainer = new PostgreSQLContainer<>();

    @DynamicPropertySource
    static void postgresqlProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", postgresqlContainer::getJdbcUrl);
        registry.add("spring.datasource.username", postgresqlContainer::getUsername);
        registry.add("spring.datasource.password", postgresqlContainer::getPassword);
    }

    @Test
    void contextLoads() {
        Assertions.assertThat(mockMvc).isNotNull();
        Assertions.assertThat(postgresqlContainer.isRunning()).isTrue();
    }
}
postgresqlContainer.getJdbcUrl()返回 jdbc:postgresql://localhost:32864/test?loggerLevel=OFF但它应该返回 jdbc:tc:postgresql://... ,它缺少 tc 部分。

有什么解决办法吗?

硬编码如下:String.format("jdbc:tc:postgresql://localhost:%s/%s", postgresqlContainer.getFirstMappedPort(), postgresqlContainer.getDatabaseName())似乎工作。

我在这里做错了什么?

最佳答案

请在此处查看橙色大警告:
https://www.testcontainers.org/modules/databases/jdbc/

您应该使用带有 tc: 的 JDBC URL前缀和 ContainerDatabaseDriver或容器实例与 getJdbcUrl()和原始驱动程序(或让系统为您检测驱动程序),而不是两者。

关于Spring testcontainers Driver org.testcontainers.jdbc.ContainerDatabaseDriver 声称不接受 jdbcUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61362092/

相关文章:

java - 在 Spring 中如何从 "<sec:authentication property="principal.username"/>"打印用户的名字和姓氏

java - 使用 Spring Boot Maven 插件时无法将资源文件夹放入 jar

java - 在 Spring Boot 中跳过 VPN DNS 可用性的集成测试

java - 使用 HSQLDB 测试日期值

java - Spring Data Jpa findBy... 和 findBy...Equals 之间的区别

java - Kafka 消费者手册提交偏移量

java - 使用 Spring Boot 和 Liquibase 时如何在每次集成测试后清理数据库表?

azure - Azure Function 项目的集成测试项目

java - spring cloud端口号,让选择变得智能

java - 在 Reactor 应用程序中测试对 Flux 的背压应用