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

Показать описание
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
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
Комментарии