I'm trying to implement the usage of Testcontainers as an alternative for IT test on a project, currently the project it's using H2 as datasource for testing, the matter is that no matter what I try I'm not able to override those configurations in order to create a custom datasource by using a DB container, what I've tried so far:
- Setting the datasource attributes directly on the application-testcontainers.yml file + @ActiveProfiles("testcontainers") annotation.
spring:
datasource:
url: jdbc:postgresql://localhost:5432/testcontainers
username: user
password: 123456
@RunWith(MockitoJUnitRunner.class)
@ActiveProfiles("testcontainers")
public final class MyTestClass {
}
- Creating an extension class in order to set the datasource attributes (overriding the beforeAll method):
public class TestcontainersExtension implements BeforeAllCallback, AfterAllCallback {
private PostgreSQLContainer<?> container;
@Override
public void beforeAll(ExtensionContext context) throws Exception {
container = new PostgreSQLContainer<>("postgres:9.4")
.withDatabaseName("testcontainers")
.withUsername("user")
.withPassword("123456")
.withExposedPorts(5432);
container.start();
System.setProperty("spring.datasource.url", container.getJdbcUrl());
System.setProperty("spring.datasource.username", container.getUsername());
System.setProperty("spring.datasource.password", container.getPassword());
}
@Override
public void afterAll(ExtensionContext context) throws Exception {
}
}
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(TestcontainersExtension.class)
public final class MyTestClass {
}
- Using @DynamicPropertySource method in order to set the configuration properties dynamically
@RunWith(MockitoJUnitRunner.class)
MyTestClass {
@Container
static PostgreSQLContainer<?> container =
new PostgreSQLContainer<>("postgres:9.4")
.withDatabaseName("testcontainers")
.withUsername("user")
.withPassword("123456")
.withExposedPorts(5432);
@DynamicPropertySource
static void setTestProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", () -> container.getJdbcUrl());
registry.add("spring.datasource.username", () -> container.getUsername());
registry.add("spring.datasource.password", () -> container.getPassword());
}
}
- @ContextConfiguration + an initializer class, also trying to set the properties dynamically
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(initializers = {MyTestClass.Initializer.class})
public final class MyTestClass {
@Container
static PostgreSQLContainer<?> container =
new PostgreSQLContainer<>("postgres:9.4")
.withDatabaseName("testcontainers")
.withUsername("user")
.withPassword("123456")
.withExposedPorts(5432);
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + container.getJdbcUrl(),
"spring.datasource.username=" + container.getUsername(),
"spring.datasource.password=" + container.getPassword()
).applyTo(applicationContext.getEnvironment());
}
}
}
None of the above worked for me, I’ll appreciate any hint or alternative.