现有的 Eclipse 项目使用 Maven,但不了解 JUnit。我应该/可以将 JUnit 集成到现有项目中,还是应该创建一个专用于 JUnit 的新项目,或者是否有更好的选择?
最佳答案
您可以通过在 pom.xml
中包含以下依赖项来将 JUnit5 添加到该项目:
<properties>
<junit.jupiter.version>5.0.1</junit.jupiter.version>
<junit.platform.version>1.0.1</junit.platform.version>
</properties>
<!--
JUnit5 dependencies:
* junit-jupiter-api: for writing JUnit5 tests
* junit-jupiter-engine: for running JUnit5 tests
* junit-platform-xxx: the foundation for JUnit5
* (Optionally) you might want to include junit-vintage-engine for running JUnit4 tests
-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
要使 Maven Surefire 能够运行 JUnit5 测试,只需在 pom.xml
中包含以下插件定义:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<excludes>
<exclude>**/MongoPopulatorTool.java</exclude>
</excludes>
</configuration>
<dependencies>
<!-- integrates JUnit5 with surefire -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<!-- ensures that a JUnit5-aware test engine is available on the classpath when running Surefire -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
</dependencies>
</plugin>
最后,要使 Eclipse 的测试运行程序能够运行 JUnit5 测试,您必须运行 Eclipse Oxygen.1a (4.7.1a) 版本(或更高版本),请查看 Eclipse docs .
关于eclipse - 如何将 JUnit 5 与现有 Eclipse 项目集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49039215/