The properties from the default spring boot application.properties can be overriden using @TestPropertySource
annotation. Spring-Boot provides a convenient way to manage application properties through the application.properties file. However, during JUnit testing, it may be necessary to override these default settings to test specific scenarios. In this blog post, we will discuss how to override the default application.properties settings in a JUnit test.
Solution 1
In the example below the default spring boot application.properties are overridden using the annotation @ @TestPropertySource
.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = {
"property.name=overridden value"
})
public class ExampleTest {
@Test
public void test() {
// test code here
}
}
Explanation:
- The @RunWith(SpringRunner.class) annotation is used to run the test with the SpringRunner.
- The @SpringBootTest annotation is used to load the Spring Boot application context.
- The @TestPropertySource annotation is used to specify a properties file or properties to be used for the test. In this example, we are overriding the “property.name” property with the value “overridden value”.
Solution 2
In the example below, the class path of the properties file is added in the annotation @ @TestPropertySource
. All the properties available in the properties file can be used in the JUnit class.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = "classpath:test.properties")
public class ExampleTest {
@Value("${property.name}")
private String propertyName;
@Test
public void test() {
System.out.println("Property name: " + propertyName);
// test code here
}
}
In this example, we are using the @TestPropertySource(locations = "classpath:test.properties")
annotation to specify a specific properties file located on the classpath for our test. This file, named test.properties
, will contain the properties we want to override for our test. Additionally, we are using the @Value
annotation to inject the value of the property.name
property into a variable named propertyName
, which we can then use in our test.
Solution 3
Another way to achieve this is by using the SpringApplication.setDefaultProperties(properties)
method in the test class.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleTest {
@Value("${property.name}")
private String propertyName;
@Test
public void test() {
Map<String, Object> properties = new HashMap<>();
properties.put("property.name", "overridden value");
SpringApplication.setDefaultProperties(properties);
System.out.println("Property name: " + propertyName);
// test code here
}
}
By using this method, we can set the default properties for the entire test class.
Solution 4
It’s important to note that the priority of the properties loading is the following:
- Properties defined in command line arguments
- Properties defined in the
spring.config.name
andspring.config.location
properties in application.properties or application.yml - Properties defined in
TestPropertySource
- Properties defined by
SpringApplication.setDefaultProperties
- Properties defined in application.properties or application.yml
In any case, the priority of loading the properties can be controlled by the user.
Solution 5
The following are recommended to use in your program.
- If you are using the
@TestPropertySource
annotation to override properties, it’s a good practice to keep the test properties file in a separate folder, such assrc/test/resources
, to clearly differentiate it from the main application properties file. - If you want to test different scenarios with different property values, you can create multiple test properties files with different names and use the
@TestPropertySource
annotation to specify the appropriate file for each test. - If you are using the
SpringApplication.setDefaultProperties(properties)
method, make sure to clear the default properties after each test by callingSpringApplication.setDefaultProperties(null)
orSpringApplication.setDefaultProperties({})
to avoid any unexpected behavior in subsequent tests. - When using the
@Value
annotation to inject property values into variables, make sure to add thespring-test
dependency to your project, as it provides the necessary support for property injection in tests. - Keep in mind that any properties that are overridden in the test will only apply to that specific test and will not affect the rest of the application. This allows you to test specific scenarios without having to change the main application properties.
By following these tips, you can effectively override default Spring-Boot application.properties settings in JUnit tests and create more flexible and efficient tests for your application.
Conclusion
In this blog post, we discussed how to override default Spring-Boot application.properties settings in a JUnit test. By using the @TestPropertySource annotation, we can easily override specific properties for our tests without modifying the original application.properties file. This allows for more efficient and flexible testing.