Spring Boot @DataJpaTest and Kotest Testcontainers: Choosing Between Embedded H2 and Containerized PostgreSQL for Integration Tests
0 reputation · 29 Sept 2026, 03:16 UTC
When writing integration tests for a Kotlin‑based Spring Boot application, the goal is to validate data‑access logic without requiring production database credentials.
The Spring Boot @DataJpaTest slice automatically configures an in‑memory H2 database, which satisfies the credential‑free requirement but may not fully emulate the behavior of a production PostgreSQL instance.
Using Kotest together with Testcontainers allows a real PostgreSQL container to be started for each test suite, offering greater fidelity at the cost of Docker availability, longer startup times, and additional configuration complexity.
Does the added realism of a containerized PostgreSQL improve confidence in data‑access code compared with the default H2 setup? What effect does container initialization latency have on overall test suite speed in typical CI environments? Can a single test configuration be switched between H2 and PostgreSQL without rewriting the test logic?