Spring TestPropertySource - Run Unit Tests with Different Properties Files
- Details
- Written by Nam Ha Minh
- Last Updated on 27 March 2023   |   Print Email
You know, a Spring Boot application usually comes with a configuration file, e.g. application.properties. And in Spring-based project development, you may want to run unit tests (e.g. Spring Data JPA Tests) with a different properties file that is specific to test environment, whereas the main properties file is for production environment.
For example, the tests should run with local database and the application should run with real database. Below is the content of the configuration file used in production (application.properties):
spring.datasource.url=jdbc:mysql://serverhostname:3306/productiondb spring.datasource.username=username spring.datasource.password=password spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=false spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
And the configuration file used in development (test) is as follows (test_local.properties):
spring.datasource.url=jdbc:mysql://localhost:3306/testdb spring.datasource.username=username spring.datasource.password=password spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
Spring framework provides the @TestPropertySource annotation which you can use in your test class to specify exactly which config file should be loaded by the test runner. Below is an example showing how to use this annotation:
package net.codejava; ... import org.springframework.test.context.TestPropertySource; @DataJpaTest @AutoConfigureTestDatabase(replace = Replace.NONE) @Rollback(false) @TestPropertySource("classpath:test_local.properties") public class ProductRepositoryTests { @Autowired private ProductRepository repo; @Test public void testListAll() { ... } @Test public void testAdd() { ... } }
You can see the TestPropertySource annotation is used:
@TestPropertySource("classpath:test_local.properties")
In this case, the test_local.properties file should be in the src/main/resources directory.
If the properties file is in different location, you must specify the relative path to the file as follows:
@TestPropertySource("classpath:/net/codejava/test_local.properties")
In this case, the test runner will look for the config file under the package net.codejava.
If you use the TestPropertySource annotation without any value, it will look for the properties file in the same location as the test class. For example:
... @TestPropertySource public class UserRepoTests { ... }
In this case, the test runner expects that the UserRepoTests.properties file should be present in the same directory as the test class.
You can also specify multiple properties file like this:
@TestPropertySource(locations = {"classpath:config1.properties", "classpath:config2.properties"})
Or:
@TestPropertySource({"classpath:config1.properties", "classpath:config2.properties"})
Those are some examples about using the TestPropertySource annotation in Spring framework in order to run unit tests with different configuration files. I hope you find this post helpful.
Watch the following video to see the coding in action:
Reference:TestPropertySource Javadocs
Other Spring and Database Tutorials:
- Spring Data JPA EntityManager Examples (CRUD Operations)
- JPA EntityManager: Understand Differences between Persist and Merge
- Understand Spring Data JPA with Simple Example
- Spring Data JPA Custom Repository Example
- Spring MVC with JdbcTemplate Example
- How to configure Spring MVC JdbcTemplate with JNDI Data Source in Tomcat
- Spring and Hibernate Integration Tutorial (XML Configuration)
- Spring MVC + Spring Data JPA + Hibernate - CRUD Example
Comments