如何将JUnit5与现有的Eclipse项目集成?
现有的Eclipse项目使用Maven,但不了解JUnit。我应该/可以将JUnit集成到现有项目中,还是应该为JUnit创建一个新项目,或者有更好的选择?您可以通过在如何将JUnit5与现有的Eclipse项目集成?,eclipse,maven,junit,junit5,Eclipse,Maven,Junit,Junit5,现有的Eclipse项目使用Maven,但不了解JUnit。我应该/可以将JUnit集成到现有项目中,还是应该为JUnit创建一个新项目,或者有更好的选择?您可以通过在pom.xml中包含以下依赖项,将JUnit5添加到该项目中: <properties> <junit.jupiter.version>5.0.1</junit.jupiter.version> <junit.platform.version>1.0.1</ju
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>
<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)版本(或更高版本),请查看。另一个答案给出了如何将JUnit添加到项目设置中的技术答案 但是很抱歉,真正的答案是:不要将单元测试添加到其他项目中。改为创建一个新的 进行开发时最重要的规则之一是。任何类/方法/xyz都应该做一件事 换句话说:您现有的eclipse项目有责任为您的“产品”提供上下文。为测试提供上下文是不同的职责 除此之外,您应该始终遵循“最佳实践”。最佳实践是在同一个项目中不使用测试和生产代码 你看,你绝对不希望你的测试源代码和你的产品代码放在同一个目录中。因此,您有两个项目,它们都可以使用相同的包,但它们的源代码位于不同的文件夹中
(您不想这样做的原因是:您只想允许您的测试依赖于您的生产代码。但是,当文件位于同一目录中时,您可能会无意中在另一个方向上创建依赖关系)虽然技术上是正确的,但我认为您在概念级别上是错误的:您没有将测试代码添加到“主”目录中项目你总是把这些东西分成不连贯的不同项目。我认为你可以在同一个项目中使用测试代码作为它正在测试的代码,尽管是在不同的代码树中(
src/test/
vs.src/main
)。项目定义应该分开。平台工件应该是版本1.2.0?maven central上没有这样的5.0.1。@dancarter是的,谢谢,我已经更正了答案。顺便说一下,漂亮的猫化身。这是什么品种?@幽灵猫谢谢!据我们所知,她是一只杂种狗。这是一次迟到的投票;-)在maven中,单元测试与被测代码位于同一个项目中,但位于不同的目录中:src/test/javaEclipse同时支持类路径分离。您可以将源文件夹标记为“仅测试”,这样就不可能在生产代码中意外引用它们。因此,Eclipse中不再需要严格分离生产代码项目和测试代码项目/片段。这很好,因为测试片段会产生额外的问题(比如不能依赖另一个具有共享测试实用程序的片段)。问题在于标准构建工具是否支持这个概念。某些IDE做什么或不做什么并不重要。或者至少不应该。