@SpringBootTest is not loading the properties from @ConfigurationProperties

1.2k Views Asked by At

When I run mvn clean install or mvn spring-boot:run, my integration test or application start is not loading properties from the application properties, but when I run the test from intelliJ Idea, or starts the server through intellij, it works fine. My codes are as below:

@Data is a lombok.Data, which generates getter and setter at compile time

package com.a.configuration;
@Data
@ConfigurationProperties(prefix = "my.service")
@Configuration
public class PropertyConfig {    
    private Integer pageSize;
    private Integer maxPageCount;
}

    package com.a.service;    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
    public class DataLoadIT {
    }

I also tried @EnableConfigurationProperties with no luck:

package com.a.configuration;    
@Configuration
@EnableConfigurationProperties(PropertyConfig.class)
public class MyConfiguration {
}

    package com.a;
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

My project structure is: src/main/java src/test/java src/integration-test/java

And using the below plugin to separate the unit test and integration-test:

    <plugin>
        <!-- run the integration tests -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.18.1</version>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.9.1</version>
        <executions>
            <execution>
                <id>add-integration-test-source-as-test-sources</id>
                <phase>generate-test-sources</phase>
                <goals>
                    <goal>add-test-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>src/integration-test/java</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>
0

There are 0 best solutions below