对我的集成测试进行了以下配置后,我遇到了以下异常: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/