I have a Spring Boot application which has some configuration properties. I'm trying to write a test for some of the components and want to load configuration properties from a test.properties
file. I can't get it to work.
Here's my code:
test.properties
file (under src/test/resources):
vehicleSequence.propagationTreeMaxSize=10000
Configuration properties class:
package com.acme.foo.vehiclesequence.config;
import javax.validation.constraints.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = VehicleSequenceConfigurationProperties.PREFIX)
public class VehicleSequenceConfigurationProperties {
static final String PREFIX = "vehicleSequence";
@NotNull
private Integer propagationTreeMaxSize;
public Integer getPropagationTreeMaxSize() {
return propagationTreeMaxSize;
}
public void setPropagationTreeMaxSize(Integer propagationTreeMaxSize) {
this.propagationTreeMaxSize = propagationTreeMaxSize;
}
}
My test:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = VehicleSequenceConfigurationProperties.class)
@TestPropertySource("/test.properties")
public class VehicleSequenceConfigurationPropertiesTest {
@Autowired
private VehicleSequenceConfigurationProperties vehicleSequenceConfigurationProperties;
@Test
public void checkPropagationTreeMaxSize() {
assertThat(vehicleSequenceConfigurationProperties.getPropagationTreeMaxSize()).isEqualTo(10000);
}
}
The test fails with "Expecting actual not to be null" meaning the property propagationTreeMaxSize
in the configuration properties class was not set.
Fortunately, Spring Boot provides a way with configuration properties. Configuration properties are nothing more than properties on beans in the Spring application context that can be set from one of several property sources, including JVM system properties, command-line arguments, and environment variables.
Basically, you: use a specific configuration to @EnableConfigurationProperties and @EnableAutoConfiguration , listing all the @ConfigurationProperties files you want to load. in the test class, you load this configuration file of tests, with an initializer class defined by Spring to load application. yml file.
We can use @SpringBootTest annotation which loads the yml file from src\main\java\com... hence when we execute the unit test, all of the properties are already there in the config properties class. @Data @Component @RefreshScope @ConfigurationProperties(prefix = "address.
properties file in the classpath (src/main/resources/application. properties).
Two minutes after posting the question, I've found the answer.
I had to enable configuration properties with @EnableConfigurationProperties(VehicleSequenceConfigurationProperties.class)
:
@RunWith(SpringRunner.class)
@TestPropertySource("/test.properties")
@EnableConfigurationProperties(VehicleSequenceConfigurationProperties.class)
public class VehicleSequenceConfigurationPropertiesTest {
@Autowired
private VehicleSequenceConfigurationProperties vehicleSequenceConfigurationProperties;
@Test
public void checkPropagationTreeMaxSize() {
assertThat(vehicleSequenceConfigurationProperties.getPropagationTreeMaxSize()).isEqualTo(10000);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With