Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration properties are not loaded in Spring Test

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.

like image 353
lexicore Avatar asked May 11 '17 08:05

lexicore


People also ask

What is configuration properties in Spring?

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.

How do you use ConfigurationProperties in a test?

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.

How do you load a YAML file in junit test cases Spring boot?

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.

Where does Spring boot look for properties file in a test?

properties file in the classpath (src/main/resources/application. properties).


1 Answers

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);
    }
}
like image 188
lexicore Avatar answered Sep 21 '22 05:09

lexicore