testcontainers / testcontainers/testcontainers-java

[Enhancement]: Document why singleton containers are required under Spring's test context caching

Offen Anfängerfreundlich
#11,967 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

type/enhancement
Vorherrschende Sprache
Java
Sterne
8.7k
Forks
1.9k
Ø Merge
2 T. 17 Std.
Gemergte PRs (30 T.)
9

Beschreibung

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:

  1. Test class A runs. The container starts. Spring builds a context pointing at the container's mapped port.
  2. Class A finishes. @Testcontainers stops the container.
  3. Class B runs. Spring reuses the cached context, still pointing at the container that was just shut down.
  4. 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Abschnitt "Singleton containers" in Manual container lifecycle control und vergleiche ihn mit der Dokumentation zu @Testcontainers und @Container auf der JUnit 5-Seite. Füge eine kurze Notiz hinzu, die erklärt, warum Container unter @SpringBootTest den Spring context cache überleben müssen und warum das Muster mit dem static initializer beim JVM exit auf Ryuk angewiesen ist; die Dokumentation ist fertig, wenn das class-to-class failure scenario und die Begründung klar sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
docker, java, spring
Bereich
documentation, testing
Issue-Typ
Dokumentation
Schwierigkeit
1/5
Geschätzter Aufwand
Unter einer Stunde
Aktivitätsstatus
Ruhig
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
85/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.