Description
openedon Jan 26, 2020
Currently, a typical test setup with Spring Boot tests and shared containers does not work anymore with JUnit 5: Using static testcontainers with Spring Boot's ApplicationContextInitializer
to configure the server's ports in the Spring context.
The problem is that the Spring application context gets initialized during Jupiter extension's post processing while testcontainers are started during before all callbacks. The latter happens after post processing.
See
https://github.com/junit-team/junit5/blob/c9ae6e261550481362f17d21e88137a8c71fc7c8/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedTestDescriptor.java#L184
vs.
https://github.com/junit-team/junit5/blob/c9ae6e261550481362f17d21e88137a8c71fc7c8/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassBasedTestDescriptor.java#L189
One approach to solve this could be to start the testcontainers also during post processing (TestInstancePostProcesso#postProcessTestInstancer
)
Then the only remaining problem would be to ensure that the TestcontainersExtension
always gets created before the SpringExtension
.
Problematic Example
@SpringBootTest
@ContextConfiguration(initializers = {Initializer.class})
@Testcontainers
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class JupiterSharedTestcontainersTests {
@Container
public static JdbcDatabaseContainer postgreSQLContainer = new PostgreSQLContainer();
@Test
public void testTomatoes() {
assertTrue("tomatoes".equals("tomatoes"));
}
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
"spring.datasource.url=jdbc:postgresql://"
+ postgreSQLContainer.getContainerIpAddress()
+ ":" + postgreSQLContainer.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT)
+ "/db_name"
).applyTo(configurableApplicationContext.getEnvironment());
}
}
}
What do you think?