intermediateTesting APIs & Dockerized Postgres

Why use Testcontainers instead of an in-memory database like H2 for integration tests?

H2 doesn't perfectly replicate Postgres-specific SQL dialects, functions, or constraint behaviors, so a query that passes against H2 in tests can still fail against real Postgres in production — a false sense of safety. Testcontainers spins up an actual throwaway Postgres Docker container for the test run, so the SQL your code executes is validated against the exact same database engine production uses.

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Spring Ecosystem Mastery library.

Why use Testcontainers instead of an in-memory database like H2 for integration tests?

Next Step

Continue to What's the difference between `@SpringBootTest` and `@WebMvcTest`?← Back to all Spring Boot questions