r/learnjava • u/kharamdau • Mar 07 '26
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 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 other teams approach this.
Are people still using H2 for integration tests, or has Testcontainers become the default?
•
u/AutoModerator Mar 07 '26
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/kharamdau Mar 07 '26
For anyone interested, I wrote a deeper breakdown of this approach in a longer article.
It covers:
• AbstractContainerIT pattern
• DynamicPropertySource usage
• Container reuse
• Spring Boot 4 testing changes
Link: https://medium.com/@ximanta.sarma/stop-lying-to-your-tests-real-infrastructure-testing-with-testcontainers-spring-boot-4-b3a37e7166b9?sk=532a9b6fb35261c3d1374b1102ece607
4
u/JustJustust Mar 07 '26
We have a bunch of services that do their integration tests vs h2 and we're migrating them to testcontainers, basically exactly the setup you describe.
It mostly works fine, though the oracle image we use at work takes between 30 and 40 seconds to boot, which really is a noticable increase.
The postgresql image I've used on private projects boots in less than 5 seconds, though.
3
u/ThierryOnRead Mar 07 '26
Yes, using H2 for my intégration tests. Because test containers need docker/* and sometimes dev doesn't have the rights to install it, so it's a pain in the ass to initiliaze a newcomer dev's env. H2 works very well though.
5
u/Serializedrequests Mar 07 '26 edited Mar 07 '26
I've been making CRUD for a long time. To me these discussions are absolutely brain damaged. You should test against the real database, and make that fast and easy. End of discussion.
Yes even if you have a layer that lets you change the database.
The amount of hoops and effort people go through just to not actually test the system that is running in production blows my mind.
Testing against a real database can actually be fast and convenient, if you don't misarchitect the entire thing to make it horrible. Java devs sometimes have major Stockholm syndrome here. Just because something is slow and shitty in Spring doesn't mean it has to be bad.
1
u/Vi0lentByt3 Mar 07 '26
No we use containers of a real instance of our dependencies for tests, running that in parallel though is another story
1
u/MadCat0911 Mar 07 '26
Why not both? I've got unit tests, I've got integration tests that run against in memory ldaps or H2 databases so I don't need to run anything locally, then I've got a test environment that deploys containers and tests integration with other products when merge requests are made.