mirror of
https://github.com/docker/docs.git
synced 2026-03-27 22:38:54 +07:00
## Description Migrate 17 Testcontainers guides from testcontainers.com into the Docker docs site, covering Java (14 guides), .NET (2 guides), and Node.js (1 guide). This follows up on PR #24450 which added the initial Go and Python guides. Each guide is converted from AsciiDoc to Hugo Markdown, split into multi-chapter stepper navigation, updated to the latest Testcontainers API, and verified with passing tests running in containers. Java guides use testcontainers-java 2.0.4 with the new 2.x Maven coordinates and package names (e.g., `testcontainers-postgresql`, `org.testcontainers.postgresql.PostgreSQLContainer`). The Quarkus guide uses Quarkus 3.22.3 with TC 1.x managed by the Quarkus BOM, since no released Quarkus version ships TC 2.x yet. ## How to test All code snippets have been verified by running each guide's source repository tests inside Docker containers with the Docker socket mounted. To re-run the verification, use the `/testcontainers-guides-migrator` skill included in this PR (`.claude/skills/testcontainers-guides-migrator/SKILL.md`). The skill's Step 6 documents the exact container commands and macOS Docker Desktop workarounds (host override, docker-java API version, etc.) needed to run each language's tests: ``` /testcontainers-guides-migrator I want you to verify all the guides in this branch. Do a full review, verifying that all code snippets compile, the code is executable, and ALL the tests pass. Run them as docker containers, never locally. ``` ## Related issues or tickets Supersedes #24450 (expanded from 2 guides to all 19) ## Reviews - [ ] Technical review - [ ] Editorial review - [ ] Product review --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
95 lines
2.7 KiB
Markdown
95 lines
2.7 KiB
Markdown
---
|
|
title: Write tests with Testcontainers
|
|
linkTitle: Write tests
|
|
description: Write your first integration test using Testcontainers for Java and PostgreSQL.
|
|
weight: 20
|
|
---
|
|
|
|
You have the `CustomerService` implementation ready, but for testing you need a
|
|
PostgreSQL database. You can use Testcontainers to spin up a Postgres database
|
|
in a Docker container and run your tests against it.
|
|
|
|
## Add Testcontainers dependencies
|
|
|
|
Add the Testcontainers PostgreSQL module as a test dependency in `pom.xml`:
|
|
|
|
```xml
|
|
<dependency>
|
|
<groupId>org.testcontainers</groupId>
|
|
<artifactId>testcontainers-postgresql</artifactId>
|
|
<version>2.0.4</version>
|
|
<scope>test</scope>
|
|
</dependency>
|
|
```
|
|
|
|
Since the application uses a Postgres database, the Testcontainers Postgres
|
|
module provides a `PostgreSQLContainer` class for managing the container.
|
|
|
|
## Write the test
|
|
|
|
Create `CustomerServiceTest.java` under `src/test/java`:
|
|
|
|
```java
|
|
package com.testcontainers.demo;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import java.util.List;
|
|
import org.junit.jupiter.api.AfterAll;
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.testcontainers.postgresql.PostgreSQLContainer;
|
|
|
|
class CustomerServiceTest {
|
|
|
|
static PostgreSQLContainer postgres = new PostgreSQLContainer(
|
|
"postgres:16-alpine"
|
|
);
|
|
|
|
CustomerService customerService;
|
|
|
|
@BeforeAll
|
|
static void beforeAll() {
|
|
postgres.start();
|
|
}
|
|
|
|
@AfterAll
|
|
static void afterAll() {
|
|
postgres.stop();
|
|
}
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
DBConnectionProvider connectionProvider = new DBConnectionProvider(
|
|
postgres.getJdbcUrl(),
|
|
postgres.getUsername(),
|
|
postgres.getPassword()
|
|
);
|
|
customerService = new CustomerService(connectionProvider);
|
|
}
|
|
|
|
@Test
|
|
void shouldGetCustomers() {
|
|
customerService.createCustomer(new Customer(1L, "George"));
|
|
customerService.createCustomer(new Customer(2L, "John"));
|
|
|
|
List<Customer> customers = customerService.getAllCustomers();
|
|
assertEquals(2, customers.size());
|
|
}
|
|
}
|
|
```
|
|
|
|
Here's what the test does:
|
|
|
|
- Declares a `PostgreSQLContainer` with the `postgres:16-alpine` Docker image.
|
|
- The `@BeforeAll` callback starts the Postgres container before any test
|
|
methods run.
|
|
- The `@BeforeEach` callback creates a `DBConnectionProvider` using the JDBC
|
|
connection parameters from the container, then creates a `CustomerService`.
|
|
The `CustomerService` constructor creates the `customers` table if it
|
|
doesn't exist.
|
|
- `shouldGetCustomers()` inserts 2 customer records, fetches all customers,
|
|
and asserts the count.
|
|
- The `@AfterAll` callback stops the container after all test methods finish.
|