spring - 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())
似乎有效。
我在这里做错了什么?
解决方案
请在此处查看大橙色警告: https ://www.testcontainers.org/modules/databases/jdbc/
您应该使用带有tc:
前缀的 JDBC URL 和ContainerDatabaseDriver
/或带有原始驱动程序的容器实例getJdbcUrl()
(或让系统为您检测驱动程序),而不是同时使用两者。
推荐阅读
- node.js - 如何在节点 alpine (docker) 中设置 oracle 即时客户端?
- node.js - 如何在所有包脚本事件之后执行 postbuild 脚本?
- android - 由于“服务获取错误”,应用程序消息无法正常工作
- javascript - 如何使用 jest 在本机反应中测试登录屏幕?
- r - 如何更改主目录,包括。SSH 位置到用户特定的文件夹?
- python - Gitlab 与 DigitalOcean 的特定运行程序在连接拨号 tcp 期间返回错误:在 67.207.67.3:53 上查找 docker:没有这样的主机
- python - 导入 matplotlib 或 seaborn 时出现 ModuleNotFoundError
- c# - Blazor - 在异步获取请求后单击按钮更新 HTML
- react-native - 使用反应原生导航的模式的 onDismiss 道具
- exchange-server - 选择应用反垃圾邮件策略的域的错误