2

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>
krmanish007
  • 6,749
  • 16
  • 58
  • 100
  • Can you share the structure of the projects you're using? – riccardo.cardin Jun 22 '17 at 08:04
  • Obviously the class marked as `@SpringApplication` has package `com.a`, hasn't it? – riccardo.cardin Jun 22 '17 at 08:12
  • 1
    Yes, but thats imported in the test. Also, same thing is working from intelliJ, but not sure why maven would not able to load! – krmanish007 Jun 22 '17 at 08:17
  • Could you move your test in question into src/test/java (rather than integration-test) and rerun? I suspect maven doesn't like the integration-test as it doesn't seem part of the default directory layout: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html – pandaadb Jun 22 '17 at 08:56
  • I tried that with no difference. After the plugin, maven is combining both the test together, and treating it as in same folder. – krmanish007 Jun 22 '17 at 08:58
  • you could try setting your properties via annotation. If that works, then maven isn't picking up your applicaiton.property file – pandaadb Jun 22 '17 at 09:22
  • 2
    where is your applicaiton.properties located? – pandaadb Jun 22 '17 at 09:32
  • That was it, thanks @pandaadb, there was a typo in my resources folder name. IntelliJ didn't had the problem as it was marked as resource root. – krmanish007 Jun 22 '17 at 09:44
  • haha okay - it's always the small things that get ya :) – pandaadb Jun 22 '17 at 10:41

0 Answers0