我的集成测试具有以下配置,但遇到以下异常: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())
似乎起作用了。
我到底做错了什么?
3条答案
按热度按时间ugmeyewa1#
请看这里的大橙子警告:https://www.testcontainers.org/modules/databases/jdbc/
您应该使用带有
tc:
前缀和ContainerDatabaseDriver
的JDBC URL,或者使用带有getJdbcUrl()
的容器示例和原始驱动程序(或者让系统为您检测驱动程序),而不是两者都使用。eoigrqb62#
在我的例子中,只是添加了postgresql依赖项(它包括驱动程序),它工作了:
我的测试类:
我希望这能对你或其他人有所帮助。
rlcwz9us3#
确保在配置文件中同时包含
dependency testcontainers postgresql
和dependency postgresql
。