Test Containers : An Integration Testing Weapon

May 11, 2025 (1d ago)

Testcontainers is a set of open source libraries that provides easy and lightweight APIs for bootstrapping local development and test dependencies with real services wrapped in Docker containers. Using Testcontainers, you can write tests that depend on the same services you use in production without mocks or in-memory services.

Sharing a powerful tool you should know about

Why Use Testcontainers?

Example

import org.junit.jupiter.api.Test;
import org.testcontainers.containers.PostgreSQLContainer;
 
public class PostgresTest {
 
    @Test
    void testDatabase() {
        try (PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15.1")) {
            postgres.start();
 
            String jdbcUrl = postgres.getJdbcUrl();
            String username = postgres.getUsername();
            String password = postgres.getPassword();
 
            // Use these credentials to run your integration test
        }
    }
}
 

⚡ Bonus: Reuse Containers for Faster Tests

In long test suites, container spin-up time can become an issue. Testcontainers offers reusable containers to persist containers across test runs—perfect for speeding up local development.

PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15.1")
        .withReuse(true);