本文へ移動
cccskills
無料GitHub で公開日本語紹介

java-testcontainers

Javaの結合テストで実際のデータベースなどをコンテナ起動し、接続や起動待ち、データ分離を整えます。Testcontainers 2.xへの移行にも対応します。

原文原文の説明を見る

Write reliable integration tests in plain Java with Testcontainers 2.x: JUnit Jupiter lifecycle, singleton containers, reuse, generic containers, wait strategies, networks, init scripts, Docker Compose, CI and Docker/Podman setup. Use when adding or fixing Testcontainers tests, or migrating from 1.x. Framework-neutral.

連携ツールDockerPostgreSQL
インストール方法を見る

こんなときに便利

  • 実DBを使う結合テストの追加
  • コンテナの起動・接続エラーの修正
  • コンテナ共有とテストデータの分離
  • Testcontainers 1.xからの移行

日本語での紹介

できること

Javaの結合テストで、データベースやメッセージ配信基盤、キャッシュをコンテナとして起動する実装を支援します。Testcontainers 2.xとJUnit Jupiterを使い、コンテナの起動・終了のタイミング、起動完了の待ち方、接続先の受け渡しを整理します。ネットワーク、初期化スクリプト、Docker Compose、CIでの実行も扱います。

こんなときに便利

本番と同じ種類・メジャーバージョンのデータベースで動作を確認したい場合に向いています。コンテナを共有しつつテストデータを分離する設計や、起動失敗の調査、1.xから2.xへの移行にも使えます。特定のアプリケーションフレームワークに依存しない内容です。

使い方の例

  • 「PostgreSQLを使う結合テストをJUnit Jupiterで追加して」
  • 「Testcontainers 1.xのテストを2.x向けに直して」
  • 「CIでコンテナの起動に失敗する原因を調べて」

注意点

ローカルやCIにDockerなどの対応環境が必要です。Testcontainers 2.xはJUnit 4に対応していません。コンテナイメージのタグは固定し、実際の認証情報は使いません。実行間でコンテナを再利用する設定はローカル専用です。JDBCドライバーが別途必要な場合もあります。

この紹介文は、公開されている SKILL.md をもとに AI(OpenAI Codex)が作成しました。正確な仕様は下の原文を確認してください。

含まれるファイル(4)

  • SKILL.md5.9 KB
  • references/containers-and-waiting.md4.3 KB
  • references/environment-ci-troubleshooting.md4.7 KB
  • references/lifecycle-and-reuse.md2.9 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

Java Testcontainers (2.x)

Run real dependencies (databases, brokers, caches) in Docker for integration tests. This skill is framework-neutral. If the project uses Spring Boot and the spring-boot-testing skill is installed, use it for @ServiceConnection, @DynamicPropertySource and test slices; for Quarkus, rely on Dev Services (Quarkus skills/docs), not hand-wired containers.

Workflow

  1. Detect the existing version (pom.xml / build.gradle) and test framework (JUnit Jupiter). Testcontainers 2.x has no JUnit 4 support.
  2. Pick the smallest container that matches production (same DB engine and major version; never H2 as a stand-in for behavior you care about).
  3. Choose a lifecycle (table below), configure waiting, wire connection details into the code under test.
  4. Run locally and in CI; check Docker availability and speed.

Testcontainers 2.x essentials

  • Artifacts are all prefixed testcontainers-: org.testcontainers:testcontainers-junit-jupiter, testcontainers-postgresql, testcontainers-mysql, testcontainers-kafka, etc. (1.x: junit-jupiter, postgresql). Core stays org.testcontainers:testcontainers.
  • Module container classes moved to per-module packages, e.g. org.testcontainers.postgresql.PostgreSQLContainer (1.x: org.testcontainers.containers.PostgreSQLContainer). Core classes (GenericContainer, Network, wait.strategy.*) remain under org.testcontainers.containers.
  • Module container classes are no longer generic: PostgreSQLContainer, not PostgreSQLContainer<?>.
  • Use a BOM to keep versions aligned: org.testcontainers:testcontainers-bom:2.0.5 (verify the current release on Maven Central).
  • Docker Compose: use ComposeContainer (Compose v2); DockerComposeContainer is still present in 2.0.5 (older API); prefer ComposeContainer.
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.testcontainers</groupId>
      <artifactId>testcontainers-bom</artifactId>
      <version>2.0.5</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers-junit-jupiter</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers-postgresql</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.7.14</version>
    <scope>test</scope>
  </dependency>
</dependencies>

Gradle: testImplementation platform("org.testcontainers:testcontainers-bom:2.0.5"), then the same Testcontainers artifacts without versions, plus testRuntimeOnly "org.postgresql:postgresql:42.7.14". The Testcontainers module does not bundle the JDBC driver; skip the explicit driver when the project already depends on one (e.g. via Spring Boot's BOM).

Minimal test

import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.postgresql.PostgreSQLContainer;

@Testcontainers
class CustomerStoreIT {

    @Container
    static final PostgreSQLContainer POSTGRES = new PostgreSQLContainer("postgres:17-alpine");

    @Test
    void connects() throws Exception {
        try (var c = java.sql.DriverManager.getConnection(
                POSTGRES.getJdbcUrl(), POSTGRES.getUsername(), POSTGRES.getPassword())) {
            org.junit.jupiter.api.Assertions.assertTrue(c.isValid(2));
        }
    }
}

Always pin the image tag (never latest); match the production major version.

Lifecycle decision

NeedPattern
One container per test classstatic @Container field
Fresh container per test method (slow, strong isolation)non-static @Container field
One container for the whole test run (fastest)singleton holder, see references/lifecycle-and-reuse.md
Keep containers between local runswithReuse(true) + opt-in property, local only

Prefer sharing a container and isolating data (truncate/rollback, unique schema or DB per test) over restarting containers.

Checklist

  • Image tag pinned and matches production.
  • Wait strategy meaningful (module defaults are usually fine; custom for GenericContainer).
  • No fixed host ports; use getMappedPort/getHost.
  • Containers shared across classes where possible (singleton).
  • Integration tests named *IT and run by failsafe (Maven) or a separate Gradle test task, so unit tests stay fast.
  • No secrets or real credentials in test containers.
  • CI runner has Docker; log output is captured on failure.

References

  • references/lifecycle-and-reuse.md: per-class/per-method, singleton pattern, reuse, parallel runs, data isolation.
  • references/containers-and-waiting.md: modules vs GenericContainer, wait strategies, env/config, init scripts, copy files, networks, multi-container setups, Docker Compose.
  • references/environment-ci-troubleshooting.md: Docker/Podman/Colima setup, CI, private registries, performance, common errors, 1.x to 2.x migration table.

Official docs: https://java.testcontainers.org/

Upgrading to 2.x: no dedicated migration page was found on java.testcontainers.org; read the 2.0.0 release notes https://github.com/testcontainers/testcontainers-java/releases/tag/2.0.0 and all releases https://github.com/testcontainers/testcontainers-java/releases for the target version, and prefer them over this skill when they conflict.

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

コメント

ログインなしで書き込めます。使ってみた感想、質問、動かなかった環境などを気軽にどうぞ。

まだコメントはありません。最初のコメントを書いてみませんか。

送信するとすぐに公開されます。メールアドレスや電話番号などの個人情報は書かないでください。URL は 2 つまでです。

0 / 1000 文字

このフォームは reCAPTCHA で保護されており、Google のプライバシーポリシーと利用規約が適用されます。

同じリポジトリのスキル

acquire-codebase-knowledge

無料概要あり

既存プロジェクトのコードや設定を調べ、技術構成・設計・規約・テスト・懸念点などを7つの文書に整理します。根拠のファイルと不明点も明記し、引き継ぎに役立てます。

  • 初めて触るコードの全体像を知りたいとき
  • プロジェクトの引き継ぎ資料作成
  • 設計資料と実装の違いを確認
github/awesome-copilot4万2026年10月22日 更新

acreadiness-assess

無料概要あり

リポジトリのAI活用準備度をAgentRCで評価し、9つの観点のスコアや改善案をHTMLにまとめます。組織独自の採点方針にも対応します。

  • AIエージェント導入前の準備度確認
  • 評価結果から改善の優先順位を決めたいとき
  • 組織独自の基準で採点したいとき
github/awesome-copilot4万2026年10月22日 更新

実際のコードから言語や開発ルールを読み取り、AIコーディングエージェント向けの指示ファイルを生成。プロジェクト全体や領域別にルールを整理します。

  • VS Code Copilot向け指示を作成
  • 開発ルールの変更を指示に反映
  • モノレポの領域別ルールを整理
github/awesome-copilot4万2026年10月22日 更新

acreadiness-policy

無料概要あり

AgentRCの準備度評価を、組織やチームの基準に合わせて調整します。チェックの重みや合格率、除外項目を設定し、CIでの合否判定にも使えます。

  • 組織共通の評価基準づくり
  • AI関連の整備状況だけを評価
  • チェックの重要度を調整したいとき
github/awesome-copilot4万2026年10月22日 更新

ad-campaign-analyzer

無料概要あり

広告の実績データから無駄な支出と成果の高い施策を見つけ、停止・増額・検証の候補を整理します。媒体をまたぐ予算配分の見直しやA/Bテストの評価にも使えます。

  • 停止すべき広告やキーワードの特定
  • 増額候補のキャンペーンや配信対象の発見
  • A/Bテスト結果の有意性を確認したいとき
github/awesome-copilot4万2026年10月22日 更新

add-educational-comments

無料概要あり

指定したコードファイルに、構文や設計の理由を説明する学習用コメントを追加します。読む人の知識に合わせて解説の深さや繰り返し、注釈番号の有無を調整できます。

  • 既存コードに初心者向け解説を付けたいとき
  • 構文や設計を選んだ理由の説明
  • 読み手の経験に合わせたコメント調整
github/awesome-copilot4万2026年10月22日 更新

github のスキルをすべて見る

このスキルの問題を報告する