Testcontainers is a Java library that provides lightweight, disposable instances of databases, message brokers, and other infrastructure components inside Docker containers for testing purposes.
It enables integration tests with real services rather than using in-memory databases or mocks, ensuring tests are closer to production environments.
* Real Dependencies – Run actual databases, message queues, or services inside Docker containers.
* Isolated and Disposable – Each test starts with a fresh environment, ensuring no conflicts.
* Easy Integration – Works seamlessly with JUnit 4, JUnit 5, and Spring Boot.
* Supports Various Technologies – Works with PostgreSQL, MySQL, Redis, Kafka, Selenium, and more.
* Custom Containers – Supports any Docker image, allowing testing with any technology.
Instead of relying on an in-memory database like H2 for tests, Testcontainers allows running a real PostgreSQL instance in a container :
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Testcontainers
class PostgresTest {
@Container
private static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:15")
.withDatabaseName("testdb")
.withUsername("testuser")
.withPassword("testpass");
@Test
void testDatabaseConnection() {
assertTrue(postgres.isRunning());
System.out.println("PostgreSQL running on: " + postgres.getJdbcUrl());
}
}
Traditional testing approaches often rely on in-memory databases or shared test environments, leading to inconsistencies between development and production. Testcontainers addresses this by:
* Running real dependencies in Docker containers
* Providing clean, isolated test environments
* Ensuring reliable and repeatable tests
* Supporting integration and end-to-end testing
To use Testcontainers in a Maven project, include the following dependencies in your pom.xml:
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.3</version>
<scope>test</scope>
</dependency>
</dependencies>
For Gradle, add:
testImplementation 'org.testcontainers:testcontainers:1.19.3'
testImplementation 'org.testcontainers:junit-jupiter:1.19.3'
2. Writing a Simple Test with Testcontainers :
Below is an example of testing a PostgreSQL database using Testcontainers and JUnit 5:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.junit.jupiter.Container;
import static org.junit.jupiter.api.Assertions.*;
@Testcontainers
class PostgresTest {
@Container
private static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:15")
.withDatabaseName("testdb")
.withUsername("testuser")
.withPassword("testpass");
@Test
void testDatabaseConnection() {
assertTrue(postgres.isRunning());
System.out.println("PostgreSQL running on: " + postgres.getJdbcUrl());
}
}
Testcontainers supports various technologies like:
Example: Running a Redis container:
@Container
private static final GenericContainer<?> redis =
new GenericContainer<>("redis:6-alpine").withExposedPorts(6379);
Testcontainers provides powerful features beyond basic containerized testing, making it an essential tool for Java developers working on integration and end-to-end testing.
By default, Testcontainers s
Testcontainers allows running custom services by wrapping any Docker image.
@Container
static GenericContainer<?> apiContainer = new GenericContainer<>("my-custom-api:latest")
.withExposedPorts(8080)
.waitingFor(Wait.forHttp("/health").forStatusCode(200));
* This waits for the API to be healthy before running tests.
Testcontainers provides pre-configured modules for common technologies like:
* Databases: PostgreSQL, MySQL, MongoDB
* Messaging: Kafka, RabbitMQ
* Search: Elasticsearch
* Browser Testi
Testcontainers is a game-changer for Java testing, providing realistic environments, better isolation, and improved test reliability.
Want to dive deeper? Check out the official docs: Testco