【发布时间】:2018-08-24 09:01:33
【问题描述】:
目标
我正在向 Maven 构建添加集成测试。我想实现以下目标:
- 默认情况下只运行单元测试。
- 必须可以只运行集成测试。
当前状态
任务变得更加复杂,因为这是一个现有的应用程序,其 maven 模块的结构(层次结构)令人困惑。我会尽力解释。
所以我有一个聚合器 pom(超级 pom),看起来像
<groupId>com.group.id</groupId>
<artifactId>app-name</artifactId>
<modules>
<module>SpringBootApp1</module>
<module>SpringBootApp2</module>
</modules>
<packaging>pom</packaging>
<profiles>
<profile>
<id>Local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Only unit tests are run when the development profile is active -->
<skip.integration.tests>true</skip.integration.tests>
<skip.unit.tests>false</skip.unit.tests>
</properties>
</profile>
<profile>
<id>Test</id>
<properties>
<!-- Only integration tests are run when the test profile is active -->
<skip.integration.tests>false</skip.integration.tests>
<skip.unit.tests>true</skip.unit.tests>
</properties>
</profile>
</profiles>
以及一些不继承自超级 pom 的模块 pom(它们继承自 spring-boot-starter-parent)并且看起来像
<groupId>com.group.id</groupId>
<artifactId>spring-boot-app</artifactId>
<packaging>jar</packaging>
<name>Spring Boot app</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<build>
<finalName>SpringBootApp1</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<executions>
<!-- Invokes both the integration-test and the verify goals of the Failsafe Maven plugin -->
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
如您所见,我在聚合器 pom 中定义了 skip.integration.tests 和 skip.unit.tests 属性(不同配置文件的属性值不同)并尝试在模块 pom 中使用它们。
问题
当我执行mvn clean test -P Test 或mvn clean verify -P Test 来运行测试时,我看到属性没有被应用(例如,虽然skip.unit.tests 是true,但执行了单元测试)。
我可以将聚合器 pom 声明为模块 pom 的父级,也许它会解决问题,但是模块 pom 已经将 spring-boot-starter-parent 声明为它们的父级。
问题
- 是否可以使用聚合器 pom 中的属性而不使其成为父级?
- 如何使用聚合器 pom 中的属性将集成测试与单元测试分开?
ps. 我向您保证,我遵守命名约定:单元测试命名为*Test.java,集成测试命名为*IT.java。所以 maven-failsafe-plugin 和 maven-surefire-plugin 将它们区分开来。
【问题讨论】:
标签: java maven spring-boot properties integration-testing