testcontainers / testcontainers/testcontainers-java

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

Open Beginner friendly
#11,967 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type/enhancement
Dominant language
Java
Stars
8.7k
Forks
1.9k
Avg merge
2d 17h
Merged PRs (30d)
9

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the "Singleton containers" section in Manual container lifecycle control and compare it with the JUnit 5 page's @Testcontainers and @Container documentation. Add a short note explaining why containers under @SpringBootTest must outlive Spring's context cache and why the static-initialiser pattern relies on Ryuk at JVM exit; the documentation is done when the class-to-class failure scenario and rationale are clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java, spring
Domain
documentation, testing
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
85/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.