gpt4 book ai didi

java - Eclipse JAVA 分离单元测试和集成测试

转载 作者:行者123 更新时间:2023-11-30 08:23:29 24 4
gpt4 key购买 nike

我正在升级一个只包含用 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 作为源文件夹?

**这是我的构建器插件配置文件

<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>

非常感谢!

最佳答案

解决此类问题一般有3种方法。

使用您已经决定使用的单独文件夹进行集成测试。

.
|-- 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阶段正确执行集成测试构建生命周期。

关于java - Eclipse JAVA 分离单元测试和集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23635120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com