0Pricing
Java Academy · Lesson

Testcontainers: Real Database Integration Tests

Spin up PostgreSQL in Docker with Testcontainers and run integration tests against a real database.

Why Testcontainers?

H2 in-memory databases don't support PostgreSQL-specific features (JSON, full-text search, ON CONFLICT). Testcontainers spins up real Docker containers during tests, giving you a production-identical database.

Adding Testcontainers Dependency

Add the Testcontainers BOM and PostgreSQL module to your build file. Spring Boot 3.1+ includes a managed Testcontainers version.

// build.gradle:
testImplementation "org.testcontainers:junit-jupiter"
testImplementation "org.testcontainers:postgresql"
// Optionally use Spring Boot Testcontainers support:
testImplementation "org.springframework.boot:spring-boot-testcontainers"

All lessons in this course

  1. Parameterized Tests with @CsvSource and @MethodSource
  2. Mockito Advanced: Argument Captors and Spies
  3. Spring Boot Test Slices: @WebMvcTest and @DataJpaTest
  4. Testcontainers: Real Database Integration Tests
← Back to Java Academy