Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

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

未關閉 適合新手
#11,967 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 2 天內回覆

@zakaullah075 已經在處理了。

開始於 2026年8月14日。

  • #11968 來自 @zakaullah075 —— 未關閉

評估

難度
1/5
預估耗時
1 小時以內
新手友好度
85/100
Issue 類型
文件
描述清晰度
描述清楚
活躍度
冷清
技術堆疊
docker, java, spring

研究方向

從 Manual container lifecycle control 中的 "Singleton containers" 區段開始,並將其與 JUnit 5 頁面上關於 @Testcontainers 和 @Container 的文件進行比較。新增一則簡短說明,解釋為什麼 @SpringBootTest 下的容器必須比 Spring 的 context cache 存活更久,以及為什麼 static-initialiser pattern 依賴 JVM exit 時的 Ryuk;當 class-to-class failure scenario 及其理由清楚明確時,文件就完成了。

由索引模型根據 Issue 內容生成。

描述

type/enhancement
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.

主要語言
Java
星號
8.8k
分支
1.9k
平均合併
4 天 1 小時
30 天內合併 PR
7

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

testcontainers/testcontainers-java 的其他 Issue

查看 testcontainers/testcontainers-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。