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.
Capabilities
Testcontainers 2.x essentials
Minimal test
Always pin the image tag (never latest); match the production major version
Lifecycle decision
Prefer sharing a container and isolating data (truncate/rollback, unique schema or DB per test) over…
Getting started
Full instructionsInstall the skill, then ask your AI tool for the task in plain words. The full SKILL.md below shows what it covers.
Instructions: SKILL.mdv1.0.0
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
- Detect the existing version (
pom.xml/build.gradle) and test framework (JUnit Jupiter). Testcontainers 2.x has no JUnit 4 support. - Pick the smallest container that matches production (same DB engine and major version; never H2 as a stand-in for behavior you care about).
- Choose a lifecycle (table below), configure waiting, wire connection details into the code under test.
- 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 staysorg.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 underorg.testcontainers.containers. - Module container classes are no longer generic:
PostgreSQLContainer, notPostgreSQLContainer<?>. - 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);DockerComposeContaineris still present in 2.0.5 (older API); preferComposeContainer.
<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
| Need | Pattern |
|---|---|
| One container per test class | static @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 runs | withReuse(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
*ITand 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 vsGenericContainer, 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.
Explore more skills
Browse all- Generate ImageGenerate images using AI. Use when asked to generate, create, or make images, textures, icons, sprites, artwork, visual…
- Anti UI SlopStop coding agents from shipping generic UI. Use UIZZE's 800,000+ real web and iOS screens to build product-specific…
- Security ReviewAI-powered codebase security scanner that reasons about code like a security researcher — tracing data flows,…
- Dotnet Best PracticesEnsure .NET/C# code meets best practices for the solution/project.
- Convert PDF to MdConverts PDF (.pdf) documents into Markdown so their contents can be accurately analyzed, summarized, searched, or…
- Meeting MinutesGenerate concise, actionable meeting minutes for internal meetings. Includes metadata, attendees, agenda, decisions,…
Security
Signing
SignedSLSA L2- Signed by
- skills-hub.ai distributor
- Method
- Distributor-signed by skills-hub.aiSigned with the skills-hub.ai Ed25519 distributor key over this exact slug, version and content. Check it with `npx @skills-hub-ai/cli verify`.
- Key
- skills-hub-ed25519-2026-11
- Signed
Reviews (0)
More from GitHub Copilot
View source →You might also like
- GuideGitHub Copilot Browser Tools Are GA: AI Agents Now Verify Their Own Front-End WorkAs of July 1, 2026, GitHub Copilot's browser tools hit general availability in VS Code — letting AI coding agents drive a real browser to verify their own front-end changes. Here's what changed, how it works, and how to apply the same pattern in Claude Code and Cursor.7 min read
- GuideKimi K2.7 Code in GitHub Copilot: The First Open-Weight Model in a Mainstream AI Coding AssistantMoonshot AI's Kimi K2.7 Code landed in GitHub Copilot on July 1 — the first open-weight model in the five-lab model picker. MIT-licensed weights on Hugging Face, hosted on Azure, billed at GPT-5.4 mini tier pricing. Here's what it means for your workflow.7 min read
- GuideGitHub Copilot AI Credits: First-Week Reality Check (June 2026)On June 1, GitHub Copilot dropped flat-rate billing and switched to token-based AI Credits. One week in: some developers report bills jumping from $29 to $750/month. Here's the math, who gets hit hardest, and how to stay in budget.8 min read
Frequently asked questions about java-testcontainers
What does the java-testcontainers skill do?
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. It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.
How do I install the java-testcontainers skill?
Run `npx @skills-hub-ai/cli install github-copilot-java-testcontainers` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/github-copilot-java-testcontainers/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.
Which AI tools does java-testcontainers work with?
java-testcontainers runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.
Is the java-testcontainers skill free?
Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.
How do I use java-testcontainers after installing it?
In Claude Code, type `/github-copilot-java-testcontainers` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.
Can I share the java-testcontainers skill with my team?
Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.
Badge for your README
Show that this skill is on Skills Hub. The badge links back to this page and shows the live install count.
[](https://skills-hub.ai/skills/github-copilot-java-testcontainers)<a href="https://skills-hub.ai/skills/github-copilot-java-testcontainers"><img src="https://skills-hub.ai/api/badges/skill/github-copilot-java-testcontainers" alt="Install on Skills Hub"></a>More options in the badge docs.