Let's learn together how to use H2 database for integration testing, using different application properties for testing and production app. Before that first learn about:
What is Unit test and Integration test?
What is H2 database?
Unit testing is an essential part of software development. It helps to ensure that our code is working as expected and it is free of bugs. And Integration testing is also a type of software testing in which the units, modules, and components of a software are integrated and tested as a cohesive unit.
Essentially Integration testing is a higher level than Unit testing. With Unit testing, we only validate if software components function well as a single unit. With Integration testing, we want to see HOW those units work together and find out if there are code conflicts between them.
Introduce the H2 database:
The H2 database is an in-memory, lightweight, and open-source database that is commonly used for development and testing purposes. H2 is a relational database management system (RDBMS) with multiple benefits that operate in memory. Its lightweight design assures that it won’t add more complexity to our application, which is one of its biggest benefits. H2 is also quite effective, enabling our application to process data fast and efficiently.
So H2 is an in-memory database that is often used for testing the application. It is easy to set up and use, and it provides a fast and reliable way to test our code. In this guide, I’ll walk you through the process of setting up our Spring Boot application for testing with H2 Database, ensuring that our actual database remains unaffected.
1. Adding H2 Database Dependency
To begin, include the H2 database dependency in your project’s build file. This dependency should be scoped for testing purposes only.
2. Configuring Test Application Properties
Create a separate properties file for testing. This file, typically named application-test.properties, allows you to define specific configurations for your test environment.
Setting Up application-test.properties:
Create the file in the src/test/resources directory.
Specify the H2 database URL, username, and password.
3. Add data.sql file
Next we need to insert the sample data into H2 database, for us to achieve this we need to create data.sql file inside the resources folder of our test folder.
Note:
The SQL stands for Structured Query Language and this data.sql file is used to insert and modify information in a database.
In the below screenshot we can see data.sql file and data which is related to test modules added inside the data.sql file.
Note: When we run the application the H2 database first look for the data.sql file to create a tables and preload the data.
4. Configure application-test.properties in Integration test
Add @TestPropertySource annotation above the Integration test class and run the test.
Note: @TestPropertySource is a class-level annotation that is used to configure the locations of properties files.
Conclusion
By following these steps, you can configure your Spring Boot application to use the H2 in-memory database for testing. This setup ensures your tests run in an isolated environment, preventing any impact on your production database and server.
Комментарии