如何将 JUnit 5 与现有的 Eclipse 项目集成?
How to integrate JUnit 5 with an existing Eclipse project?
现有的Eclipse 项目使用Maven 但不了解JUnit。 Should/can 我将 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 的测试 运行ner 到 运行 JUnit5 测试,您必须 运行ning Eclipse Oxygen.1a (4.7.1a) 版本(或更高版本) , 看看 Eclipse docs.
另一个答案给出了如何将 JUnit 添加到项目设置中的技术答案。
但是抱歉,真正的答案是:不要添加您的单元测试到您的其他项目。创建一个 new 代替。
进行开发时最重要的规则之一是 Single Responsibility Principle。任何 class/method/xyz 都应该做 一件 事情。
换句话说:您现有的 eclipse 项目有责任为您的 "product" 提供上下文。为测试提供上下文是一项不同的责任。
除此之外,您应该始终遵循 "best practices"。最佳实践再次是 not 在同一个项目中有测试和生产代码。
你看,你绝对不希望你的测试源代码与你的生产代码位于相同的目录中。因此,您有 两个 项目,它们都可以使用 相同的 包 - 但它们的源代码位于不同的文件夹中!
( 你不想拥有它的原因是:你只想让你的测试依赖于你的生产代码。但是当文件位于同一目录中时,你可能会无意中在另一个方向上创建依赖关系)
现有的Eclipse 项目使用Maven 但不了解JUnit。 Should/can 我将 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 的测试 运行ner 到 运行 JUnit5 测试,您必须 运行ning Eclipse Oxygen.1a (4.7.1a) 版本(或更高版本) , 看看 Eclipse docs.
另一个答案给出了如何将 JUnit 添加到项目设置中的技术答案。
但是抱歉,真正的答案是:不要添加您的单元测试到您的其他项目。创建一个 new 代替。
进行开发时最重要的规则之一是 Single Responsibility Principle。任何 class/method/xyz 都应该做 一件 事情。
换句话说:您现有的 eclipse 项目有责任为您的 "product" 提供上下文。为测试提供上下文是一项不同的责任。
除此之外,您应该始终遵循 "best practices"。最佳实践再次是 not 在同一个项目中有测试和生产代码。
你看,你绝对不希望你的测试源代码与你的生产代码位于相同的目录中。因此,您有 两个 项目,它们都可以使用 相同的 包 - 但它们的源代码位于不同的文件夹中!
( 你不想拥有它的原因是:你只想让你的测试依赖于你的生产代码。但是当文件位于同一目录中时,你可能会无意中在另一个方向上创建依赖关系)