【问题标题】:How to integrate JUnit 5 with an existing Eclipse project?如何将 JUnit 5 与现有的 Eclipse 项目集成?
【发布时间】:2018-08-08 21:40:04
【问题描述】:

现有的 Eclipse 项目使用 Maven 但不了解 JUnit。我应该/可以将 JUnit 集成到现有项目中,还是应该创建一个专门用于 JUnit 的新项目,还是有更好的选择?

【问题讨论】:

  • 顺便说一句,猫头像不错。这是什么品种?
  • @GhostCat 谢谢!据我们所知,她是一只杂种狗。
  • 迟到了 ;-)

标签: eclipse maven junit junit5


【解决方案1】:

您可以通过在 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

【讨论】:

  • 虽然技术上正确,但我认为您在概念层面上是错误的:您确实将测试代码添加到“主要”项目。你总是把这些东西分成分离的不同项目。
  • 我认为您可以在与正在测试的代码相同的项目中拥有测试代码,尽管在不同的代码树中(src/test/src/main)。
  • 您在技术上可以做到这一点并不意味着您应该这样做。 project 定义应该分开。
  • 平台工件应该是1.2.0版本? maven central 上没有这样的 5.0.1。
  • @dancarter 是的,谢谢,我已经更正了答案。
【解决方案2】:

另一个答案给出了如何将 JUnit 添加到项目设置中的技术答案。

但是很抱歉,真正的答案是:不要将您的单元测试添加到您的其他项目中。改为创建一个新的

进行开发时最重要的规则之一是Single Responsibility Principle。任何类/方法/xyz 都应该做一件事

换句话说:您现有的 eclipse 项目有责任为您的“产品”提供上下文。为测试提供上下文是不同的责任。

除此之外,您还应该始终遵循“最佳做法”。最佳做法是再次在同一个项目中拥有测试和生产代码。

您知道,您绝对希望您的测试源代码与您的生产代码位于 same 目录中。因此,您有 两个 项目,它们都可以使用 相同 包 - 但它们的源代码位于不同的文件夹中!

(您不想这样做的原因:您只想让您的测试依赖于您的生产代码。但是当文件位于同一目录中时,您可能会无意中在另一个方向创建依赖项)

【讨论】:

  • 在 maven 中,单元测试与被测代码在同一个项目中,但在不同的目录中:src/test/java
  • Eclipse 同时支持类路径分离。您可以将源文件夹标记为“仅测试”,这样就不会在生产代码中意外引用它们。因此,Eclipse 不再需要严格分离生产代码项目和测试代码项目/片段。这很好,因为测试片段会产生额外的问题(比如不能依赖另一个片段与共享的测试实用程序)。
  • 问题是标准构建工具是否支持该概念。某些 IDE 做什么或不做什么并不重要。或者至少不应该。
猜你喜欢
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多