Having the following configuration for my integration tests I ran into the following exception:
Driver org.testcontainers.jdbc.ContainerDatabaseDriver claims to not accept jdbcUrl, jdbc:postgresql://localhost:32864/test?loggerLevel=OFF`
AbstractIntegrationTest.java:
@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();
}
}
The postgresqlContainer.getJdbcUrl()
returns jdbc:postgresql://localhost:32864/test?loggerLevel=OFF
But it should return jdbc:tc:postgresql://...
, its missing the tc part.
Any solution to this?
Hardcoding it like: String.format("jdbc:tc:postgresql://localhost:%s/%s", postgresqlContainer.getFirstMappedPort(), postgresqlContainer.getDatabaseName())
seems to work.
What am I doing wrong here?
Please see the big orange warning here: https://www.testcontainers.org/modules/databases/jdbc/
You should use either the JDBC URL with tc:
prefix and ContainerDatabaseDriver
or container instance with getJdbcUrl()
and the original driver (or let the system detect the driver for you), not both.