Video in progress

Repository tests use H2 while prod runs PostgreSQL. How do you build a database testing strategy at the organization level?

I don't trust testing PostgreSQL-specific code on H2. I switch to a real PostgreSQL with Testcontainers, in exactly the prod version and with exactly the Flyway migrations. For speed, container reuse and proper layering of tests, with a separate check for the migrations.

📖 Answer

Even with "PostgreSQL mode", H2 is a different database: jsonb, ON CONFLICT, partial indexes, SKIP LOCKED, exclusion constraints, parts of window functions, isolation and lock behavior all differ or don't exist. The result: tests are green and prod fails. Or the reverse: the team gives up PostgreSQL features because "it doesn't work in H2".

The strategy I propose:

@SpringBootTest
@Testcontainers
class OrderRepositoryIT {
    @Container
    @ServiceConnection   // Spring Boot 3.1+: supplies the URL/login automatically
    static PostgreSQLContainer<?> db = new PostgreSQLContainer<>("postgres:17-alpine");
}
  • One version. The Docker image tag is the same as prod and stored in a shared BOM or configuration; teams don't pick their own.
  • The schema through migrations. Flyway, not ddl-auto=create, so the tests check the migrations too.
  • Speed. A static container, one for the whole test class or suite; reusable containers for local runs; each test isolated with a @Transactional rollback or table cleanup.
  • Layers. Business logic: unit tests without a database. Repositories and SQL: Testcontainers. For several concurrent transactions (locks, deadlocks, SKIP LOCKED), separate tests with real threads.
  • A migration test in CI: all the migrations on an empty database, plus the new migrations on the previous release's schema.

CI needs Docker, which is the platform team's job: Docker on the runners, or Testcontainers Cloud. It's done once and serves every team.