Spring Boot makes use of the database specified in the application properties file. When the application is tested with test cases, the spring boot application uses the same database configuration to create and modify data. If you plan to clean and create a new database for testing purposes, this will have an impact on the database you are currently using. This will be critical if you attempt to debug test cases in the production environment. The data in the production database will be either deleted or modified.
Spring boot allows you to connect to different database configurations for testing purposes. When you run the test cases, it always creates a database connection with only the test database configured. In this case, the risk of deleting or modifying the actual database is eliminated. The test configured database can be dropped and recreated without affecting the actual database. In this section, we will look at how to create and configure different databases for testing the spring boot application.
Using test resource folder
The configuration and properties from the source folder src/main/resources are used by the Spring Boot application. This folder contains the application properties files. When the spring boot application starts, it loads the application properties files from this source folder and uses them within the application.
By default, the test cases will use the properties from the source folder. The properties files in the test resources folder override these properties files in the spring boot application. The files in the src/test/resources directory will be picked and used while running the test cases.
src/main/resource/application.properties
spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
src/test/resource/application.properties
spring.datasource.url=jdbc:mysql://localhost/testdb
spring.datasource.username=root
spring.datasource.password=testpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
Using spring boot profile
Spring boot profile will be used to run the test against a different database. While running the test cases, the spring boot profile should be added. Two application properties files with names like application.properties and application-test.properties will be created. The name of the spring boot profile will be suffixed with the name of the properties file. The profile name must be specified when the spring boot application is run.
src/main/resources/application.propertes
spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
src/main/resources/application-test.properties
spring.datasource.url=jdbc:mysql://localhost/testdb
spring.datasource.username=root
spring.datasource.password=testpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
How to run the profile based application
When the application test cases are run, the following command should be used to select the appropriate database for testing. The spring boot application will run with the profile specified in the command line, and the properties files suffix will be appended to the profile name. If the spring boot application cannot find the properties file suffix with the profile name, it will load the default properties file.
mvn spring-boot:run -Dspring.profiles.active=test
To select the properties files, you can also use the annotation @ActiveProfiles(“test”) in the test classes. The annotation @ActiveProfiles(“test”) must be added to the declared test class. The test case class will not be executed if the active profile name does not match the spring boot profile name. As a result, the risk of running the test case code in a valid database is eliminated.
package com.yawintutor.repository;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import com.yawintutor.model.Student;
@SpringBootTest
@ActiveProfiles("test")
public class StudentRepositoryTest {
@Autowired
StudentRepository repository;
@Test
public void studentRepositoryTest() {
......
......
......
}
}
Connecting to Multiple databases
Spring boot allows you to connect to multiple databases while running test cases. The test cases may be required to connect to a database in order to obtain data, and the current database may be used to process the data. To perform the test cases, the Spring Boot application must be connected to multiple databases. To connect multiple databases in a spring boot application, see this post. In the test environment, the same steps can be taken to connect multiple databases.
How to connect multiple databases using Spring Boot