r/SpringBoot • u/kharamdau • Mar 07 '26
How-To/Tutorial Are people still using H2 for Spring Boot integration tests in 2026?
I've been seeing something repeatedly in Spring Boot services.
Integration tests run against H2 or some mocked dependencies. Everything is green locally and in CI.
Then the first real deployment runs Flyway migrations against PostgreSQL and suddenly things break. Constraint differences, SQL dialect issues, index behavior, etc.
The tests passed, but they were validating a different system.
Lately I've been leaning toward running integration tests against real infrastructure using Testcontainers instead of H2. The feedback loop is slightly slower but the confidence is much higher.
Example pattern I've been using:
- Start a PostgreSQL container via Testcontainers
- Run real Flyway migrations
- Validate schema with Hibernate
- Share the container across test classes via a base integration test
The container starts once and the Spring context is reused, so the performance cost is actually manageable.
Curious how others are approaching this.
Are teams still using H2 for integration tests, or has Testcontainers become the default?
For context, I wrote a deeper breakdown of the approach here:
54
u/mgalexray Mar 07 '26
Not in a while. I just run Testcontainers with Postgres directly, bootstrapped by flyway and transactional tests so everything is quite fast.
7
u/Ok_Arugula6315 Mar 07 '26
Yeah we use testcontainers with base i test class to keep context between test suites
7
u/__clayton__ Mar 07 '26
We use TestContainers to boot up a MSSQL server image, but it's slow AF and we can't really figure out any meaningful fixes for this.
4
u/roiroi1010 Mar 07 '26
These days testcontainers integrate seamlessly with Spring Boot so now I always use Testcontainers. Not only for databases but for integrations with AWS services.
3
u/g00glen00b Mar 07 '26
Normally I use Testcontainers as well, but there's not always a decent containerized version available. So in those cases I think it's a valid trade-off to test with H2.
2
u/LouGarret76 Mar 07 '26
I believe h2 support postgresql dialect. I do test with h2 locally especially early dev with no authentication system.
I tend not to use flyway for new apps. I let jpa ddl do the trick until mvp. It work most of time. I use flyway in production when I want to preserve existing user data during migration.
2
u/ZERAVLA_23 Mar 07 '26
Yo si lo he llegado a usar, pero casi siempre prefiero trabajar una BD de prueba real
2
u/UnableAmphibian585 Mar 28 '26
H2 was a huge game changer in integration testing but Testcontainsrs is even bigger one.
1
u/Krangerich Mar 07 '26
Why would you even consider using H2 for production applications (unless the production database is also H2)?
The whole point of tests that are hitting the DB is that you use the same scenario. I don't see a reason why you would use H2 apart from a lack of expertise / seniority. These problems you mentioned ("runs on my machine, but not in production") have been solved 15-20 years ago.
1
u/WVAviator Mar 08 '26
Our jenkins pipeline doesn't work with test containers - I think it has something to do with it being behind the company proxy but failing to fetch Docker images, idk been awhile since we tried. We can still run those tests locally though, just not in CI.
1
u/_kisaka Mar 09 '26
What about testing the repository’s
1
u/Zchwarzer Mar 09 '26
Nope, currently I'm always use testcontainer
1
u/CursedCarb0y Apr 05 '26
I started a Spring Boot course that my school is offering, and it looked very professional to me. We are using H2 for now.I started a Spring Boot course that my school is offering, and it looked very professional to me. We are using H2 for now.
0
u/Mindless_Security744 Mar 07 '26
I gave up on writing unit/integration tests, I now deploy to a qe env and run full end to end integration automation tests outside of the code base.
6
u/[deleted] Mar 07 '26
[removed] — view removed comment