testcontainers / testcontainers/testcontainers-java
Spring Boot Tests with JUnit5 Jupiter and Shared Containers
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 8.7k
- Forks
- 1.9k
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 9
Description
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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the problematic Spring Boot/Jupiter example in the issue and compare the JUnit Jupiter ClassBasedTestDescriptor.java ordering around the referenced lines 184 and 189. Reproduce the interaction between ApplicationContextInitializer, Testcontainers, and Jupiter callbacks, then determine whether a supported ordering can make the shared-container setup work without breaking other extensions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, java, postgresql, spring-boot
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100