首页 > 解决方案 > Spring testcontainers Driver org.testcontainers.jdbc.ContainerDatabaseDriver 声称不接受 jdbcUrl

问题描述

为我的集成测试进行了以下配置后,我遇到了以下异常:

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())似乎有效。

我在这里做错了什么?

标签: springspring-bootintegration-testingtestcontainers

解决方案


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

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


推荐阅读