当我运行mvn clean install
或mvn spring-boot:run
时,我的集成测试或应用程序启动不会从应用程序属性加载属性,但是当我从intelliJ Idea运行测试或通过intellij启动服务器时,它工作正常。我的代码如下:
@Data是一个lombok.Data,它在编译时生成getter和setter
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 {
}
我也试过@EnableConfigurationProperties
而没有运气:
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);
}
}
我的项目结构是: 的src /主/ JAVA 的src /测试/ JAVA SRC /集成测试/ JAVA
使用以下插件分离单元测试和集成测试:
<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>