testcontainers / testcontainers/testcontainers-java
Spring Boot Tests with JUnit5 Jupiter and Shared Containers
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Java
- Estrellas
- 8.7k
- Forks
- 1.9k
- Merge medio
- 2 d 17 h
- PR fusionados (30 d)
- 9
Descripción
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?
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con el ejemplo problemático de Spring Boot/Jupiter del issue y compara el orden de JUnit Jupiter ClassBasedTestDescriptor.java alrededor de las líneas referenciadas 184 y 189. Reproduce la interacción entre ApplicationContextInitializer, Testcontainers y los callbacks de Jupiter; después determina si un orden compatible puede hacer que funcione la configuración del contenedor compartido sin romper otras extensiones.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- docker, java, postgresql, spring-boot
- Área
- testing-qa
- Tipo de issue
- Error
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 25/100