testcontainers / testcontainers/testcontainers-java
[Enhancement]: Document why singleton containers are required under Spring's test context caching
Nessuno ha ancora preso questa issue.
- Lingua principale
- Java
- Stelle
- 8.7k
- Fork
- 1.9k
- Merge medio
- 2g 17h
- PR unite (30g)
- 9
Descrizione
Module
Core
Proposal
The gap
The "Singleton containers" section in Manual container lifecycle control shows the static-initialiser pattern, and the JUnit 5 page documents @Testcontainers / @Container. Neither explains the failure that makes the singleton pattern necessary under @SpringBootTest, and the symptom is misleading enough that it costs people real time.
The failure
Spring caches the application context across test classes. @Testcontainers ties container lifecycle to the test class. Those two facts conflict:
- Test class A runs. The container starts. Spring builds a context pointing at the container's mapped port.
- Class A finishes.
@Testcontainersstops the container. - Class B runs. Spring reuses the cached context, still pointing at the container that was just shut down.
- Every test in class B fails with connection errors.
The misleading part is that class A passes and class B fails, and the error mentions nothing about containers. It reads like test pollution or an ordering problem, so that is where people go looking first.
Current documentation
Manual container lifecycle control says only: "Sometimes it might be useful to define a container that is only started once for several test classes." The JUnit 5 page does not mention Spring at all. So the pattern is documented, but the reason for it is not.
Proposal
Add a short note to the Singleton containers section: under @SpringBootTest, container lifecycle has to outlive Spring's context cache. That is why the containers are started in a static initialiser and deliberately never stopped — Ryuk reaps them at JVM exit, so nothing leaks.
I hit this building a Spring Boot project with Postgres and RabbitMQ containers and lost a while to it. Happy to open a PR if you would take it.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la sezione "Singleton containers" di Manual container lifecycle control e confrontala con la documentazione di @Testcontainers e @Container nella pagina di JUnit 5. Aggiungi una breve nota che spieghi perché i container sotto @SpringBootTest devono sopravvivere alla cache del contesto di Spring e perché il pattern del static initializer si affida a Ryuk all'uscita della JVM; la documentazione è completa quando lo scenario di errore class-to-class e la motivazione sono chiari.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- docker, java, spring
- Ambito
- documentation, testing
- Tipo di issue
- Documentazione
- Difficoltà
- 1/5
- Tempo stimato
- Meno di un'ora
- Stato di attività
- Tranquilla
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 85/100