Spring Boot H2 Embedded Database Example - Java Junit Test H2 Database Tutorial

preview_player
Показать описание
In a deployed environment, your application typically connects to a live relational database management system such as MySQL or PostgresSQL. When writing automated tests using jUnits, it is generally encouraged that you do not connect and test against the real live database, especially when data is in motion. This can lead to race conditions, unpredictable test behaviour and inconsistent results. Have you ever run a test once and it failed, only to run it again without any code change and it passed?

In this tutorial I show you how to code SpringBoot jUnit tests that will execute against an Embedded In-memory H2 database instead of the live RDBMS.

Another option is to spin up a real test database, create the schema, insert the initial data and run your tests against it. In your junits you can roll back transactions after the test to ensure no data from each test gets committed. This is great for testing vendor specific sql and is another option instead of switching to an in memory database.

We first create a sample Spring Boot application that connects to a MySQL database in non-local profiles & environments. The application simply has a service layer that instructs an Account Data access object to insert an account row when invoked.

Configure your maven POM file to include the in-memory h2 database during test scope and RDBMS driver as compile. When a jUnit is run, Spring Boot will automatically create a Datasource for the H2 database. A JdbcTemplate is also auto configured.

With Spring Boot 1.4 we can use the new SpringRunner RunWith class and SpringBootTest annotation. See links below for more details.

Inside the test class we first use JdbcTestUtils countRowsInTableWhere method to ensure there are 0 rows in the table with our where clause. We then call the service method and assert that there is now 1 expected row with the same where clause.

With these tests; if someone introduces a bug relating to the row insertion - the jUnit will fail during the CI/CD build and flag this issue before it is propagated through the environments. Great! Lets write more tests!

If you enjoyed this content Subscribe for more tech vids :)

Have a great day!
Philip

Рекомендации по теме
Комментарии
Автор

You're a life saver. Your solutions are so much accurate and relate-able to what we actually need

gdhameeja
Автор

Liked and subscribed. This video is a lifesaver!

NOCDIB
Автор

Excellent tutorial Philip ....Waiting for more tutorial

sucountary
Автор

I know this is an old post, but I have a Spring Boot REST app with an Oracle database, and am trying to use this same approach for my JUnit tests. I have a separate 'application-test.properties' file, where I provide the spring.datasource parameters (and included this: SCHEMA IF NOT EXISTS schema_name). As you did, in the @Before for the test, I run several executeSqlScript to create the table and then populate the table with several rows, but when I try to run the JUnit test, it throws this error:
StatementCallback; bad SQL grammar [select * from schema_name.persons where id = ?]; nested exception is org.h2.jdbc.JdbcSQLException: Table "persons" not found; SQL statement: select * from schema_name.persons where id = ?
Any ideas where I may have gone wrong?

almiller
Автор

What's the difference between ScriptUtils and @sql annotation? It appears that with mysql 'alter table' scripts ScriptUtils causes the server to hang eternally while @sql keeps running smoothly. Cannot figure out why? With H2 all works OK as well.

andrii_popov
Автор

Where were the initial shots of this vid were taken?

zaighamabbas
Автор

How do we do it spring boot integration test if we have multiple data bases in a single application

lakshmanaswamy
Автор

Short and crisp video. Good job & nice scenery in the beginning :)
Also, on you said in Spring 1.4 you dont need @Autowired annotations but I see you've used it in your Test class. Any reasons?

bhuvneshjangid
Автор

Can you provide the source code for this project. I will be thankful to you.

bikrantmahakul
Автор

I followed the example above but JdbcTemplate keeps connecting to my Postgres database instead of the H2 database. I guess it picks up the Postgres database connection parameters from the application.properties file. Any idea how to resolve this? Here are my settings on the pom file:

<dependency>


<scope>test</scope>
</dependency>
<dependency>

<artifactId>h2</artifactId>
<version>1.4.193</version>
<scope>test</scope>
</dependency>
<dependency>


<scope>runtime</scope>
</dependency>

penmark-official
Автор

Can you please provide source code...?

padmesh
Автор

Hello buudy, good video you have the code in github ???

MrCamilo
visit shbcf.ru