H2 Database Engine with Junit testing
Bonjour ! tout le monde
H2 Database Engine with testing
Bonjour ! tout le monde
Today i will write about H2 Database testing with spring boot and the problem that i had faced with my current project.
What is H2 Database? →H2 database is Java SQL database which a embedded server modes and in-memory databases.
The main features of H2 Database according to https://www.h2database.com/html/main.html
- Very fast, open source, JDBC API
- Embedded and server modes; in-memory databases
- Browser based Console application
- Small footprint: around 2 MB jar file size
For my project , we are using Spring boot and Spring Data JPA.
I will show you the structure

A java project
A pom.xml

The pom.xml of my project
We will try to use H2 database to be two type databases. Frist is a Postgres database with Hibernate (OR Mapping) and second is oracle with JDBC template. But during the implementation, we cannot initial the JDBC bean.
The first version of configuration

application-test.yml
I have got errors 1.org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker 2.org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name.
After that, we try to reseach about the problem to use the H2 databases with multiple database and multiple way to initail ( JPA Hibernate and JDBC template to connect at the same times).
The Last version of configuration
we decided to workaroud with this case by using one database with 2 schemas. You can see the configuration below:

application-test.yml (last version)

data-h2.sql

schema-h2.sql
Moreover, i will show you the configuration with Spring boot (Java)

TestConfiguration class

Testing class
From above photo , the Repository is a class that extend CrudRepository
For the information (References):
https://www.baeldung.com/spring-boot-hibernate https://www.springboottutorial.com/hibernate-jpa-tutorial-with-spring-boot-starter-jpa https://howtodoinjava.com/spring-boot2/datasource-configuration/ https://www.mkyong.com/spring-boot/spring-boot-jdbc-examples/
메타데이터
- post_id
- e8fd28bf74ca
- slug
- h2-database-engine-with-junit-testing-e8fd28bf74ca
- url
- https://medium.com/@lalida_a/h2-database-engine-with-junit-testing-e8fd28bf74ca
- canonical_url
- https://medium.com/@lalida_a/h2-database-engine-with-junit-testing-e8fd28bf74ca
- author_url
- https://medium.com/@lalida_a
- status
- ok
- fetched_at
- 2026-07-29 10:02:12