Language: Java
Testing
Testcontainers was created to simplify integration testing by providing real, isolated environments using Docker containers. It eliminates the need for complex local setup and ensures tests run consistently across different machines and CI environments.
Testcontainers is a Java library that provides lightweight, disposable Docker containers for running integration tests. It allows developers to test against databases, message brokers, or any containerized service in a reproducible way.
Add org.testcontainers:testcontainers dependency in pom.xmlAdd testImplementation 'org.testcontainers:testcontainers:1.20.3' in build.gradleTestcontainers allows developers to spin up Docker containers for databases, message queues, or other services during test execution. It supports JUnit 5 extensions, lifecycle management, reusable containers, and pre-configured modules for common services like PostgreSQL, MySQL, Kafka, and Redis.
import org.testcontainers.containers.PostgreSQLContainer;
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15.3")
.withDatabaseName("testdb")
.withUsername("user")
.withPassword("password");
postgres.start();Starts a PostgreSQL container with a database name, username, and password for integration testing.
String jdbcUrl = postgres.getJdbcUrl();
String username = postgres.getUsername();
String password = postgres.getPassword();
Connection conn = DriverManager.getConnection(jdbcUrl, username, password);Retrieves connection details from the container to connect your application to the test database.
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
public class MyDatabaseTest {
@Container
public static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15.3");
@Test
void testDatabaseConnection() {
assertNotNull(postgres.getJdbcUrl());
}
}Uses annotations to automatically manage container lifecycle with JUnit 5 tests.
postgres.withReuse(true);
// container can be reused across multiple test runs for faster executionEnables container reuse to reduce startup time during repeated test executions.
import org.testcontainers.containers.KafkaContainer;
KafkaContainer kafka = new KafkaContainer("confluentinc/cp-kafka:7.6.1");
kafka.start();Starts a Kafka container for testing messaging functionality in applications.
postgres.withInitScript("init.sql").withExposedPorts(5432);Demonstrates how to initialize a container with a custom SQL script and expose ports.
Use `@Testcontainers` and `@Container` annotations for automatic lifecycle management.
Prefer lightweight containers for faster test execution.
Reuse containers when possible to reduce startup time.
Externalize configuration and credentials to environment variables for security.
Combine Testcontainers with JUnit 5 for seamless integration testing in CI/CD pipelines.