我正在升级一个只包含JAVA中编写的集成测试的项目。
现在我们要编写单元测试,所以我决定创建src / it / java文件夹来放置所有现有的测试并在src / test / java中编写新的单元测试
我已经使用了surfire和build-helper来做到这一点。
在命令行中我可以运行
mvn clean install,只读取src / test / java目录进行测试
当我做
时mvn -Pintegration-test clean install我看到src / it / java中的测试被读取进行测试
所以在命令行中一切正常。
现在我想在eclipe上导入我的项目。当我这样做时,只有src / test / java用作源文件夹。 如何设置pom.xml以将src / test / java和src / it / java作为源文件夹?
**这是我的构建器插件conf
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>process-resources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
非常感谢!
答案 0 :(得分:4)
通常有三种解决此类问题的方法。
使用单独的文件夹进行集成测试,因为您已决定使用。
.
|-- pom.xml
`-- src
|-- it
| `-- java
| `-- com
| `-- soebes
| `-- maui
| `-- it
| `-- BitMaskIT.java
|-- main
| `-- java
| `-- com
| `-- soebes
| `-- maui
| `-- it
| `-- BitMask.java
`-- test
`-- java
`-- com
`-- soebes
`-- maui
`-- it
`-- BitMaskTest.java
要在Eclipse中正常工作,您需要使用build-helper-maven-plugin 你已经建议:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-test-source</id>
<!--
This will help m2eclipse to recognize the folder as source
folder after update project configuration.
Based on comment updated.
-->
<phase>validate</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
但正如您所看到的,这会在您的pom文件中进行多次补充配置。
除了上述内容之外,使用maven-surefire-plugin进行集成测试是完全错误的,因为maven-surefire-plugin用于单元测试,不用于集成测试,所以更好的是去使用maven-failsafe-plugin进行集成测试。
最好遵循单元和集成测试的命名约定。在 maven-surefire-plugin the unit tests follow the following convention:
- *Test.java
- *TestCase.java
- Test*.java
而maven-failsafe-plugin follow the following convention:
中的集成测试- *IT.java
- *ITCase.java
- IT*.java
.
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- soebes
| `-- maui
| `-- it
| `-- BitMask.java
`-- test
`-- java
`-- com
`-- soebes
`-- maui
`-- it
|-- BitMaskIT.java
`-- BitMaskTest.java
但有时候制作一个仅包含该模块的单独模块要好得多 集成测试将它们与单元测试分开。如果这可能会有所帮助 集成测试的配置与单元测试不同。
所以制作这样的结构:
+-- root (pom.xml)
+--- mod-it (pom.xml)
+--- mod-core (pom.xml)
您应该遵循mod-it
中的命名约定来使用maven-failsafe-plugin
并在maven的integration-test
阶段正确执行集成测试
建立生命周期。
答案 1 :(得分:-2)
您需要在Eclipse中手动添加 src / it / java 文件夹:
项目属性 - &gt; Java构建路径 - &gt; 来源标签 - &gt; 添加文件夹。