【问题标题】:Configure classpath in eclipse for JUnit 5 with Maven使用 Maven 在 Eclipse 中为 JUnit 5 配置类路径
【发布时间】:2019-03-09 22:27:17
【问题描述】:

Eclipse (2018-09) 仅当 JUnit 5 引擎存在于项目的类路径中时才支持 JUnit 5 测试。

现在我的 Maven 项目有两种可能性:

  1. 通过 Eclipse JUnit 库将其添加到项目中

    并且只将 API 添加到依赖项

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
    
  2. 将引擎和 API 添加到我的 Maven pom

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
    </dependency>
    

如果我做前者,那么每个使用 eclipse 的人都必须自己做。

如果我做后者,那么我的(测试)编译时类路径会被实现类污染,我(和其他 IDE 的用户)可能会使用它来代替 API 类。此外,这可能会导致与可能需要不同版本引擎的 IDE 发生冲突,而不是 cp 上的版本。 IIRC 这就是 API 和引擎首先被拆分的全部原因。

不幸的是,Maven 中没有 testRuntimeOnly 范围(就像 gradle 中一样)。

TLDR:为 Maven 项目配置 JUnit 5 for eclipse 的正确方法是什么?

【问题讨论】:

    标签: java eclipse maven junit5


    【解决方案1】:

    如果我做前者,那么每个使用 Eclipse 的人都必须自己做。

    我假设您打算让 Eclipse 用户有机会通过右键单击 JUnit 测试类并选择 Run as > JUnit Test 来执行测试。我不知道这是否是正确的方法,但要做到这一点,除了 JUnit Jupiter API/Engine 之外,您还需要添加一个额外的依赖项,即 JUnit Platform启动器

    例如:

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.4.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.4.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-launcher</artifactId>
      <version>1.4.2</version>
      <scope>test</scope>
    </dependency>
    

    不确定它是否相关,但我使用的是 maven-surefire-plugin 版本 2.22.1,如果 JUnit Platform Launcher 是抛出 ClassNotFoundException不见了。

    【讨论】:

    • 有效。 Maven 跟不上新的 JUnit 库的麻烦。
    猜你喜欢
    • 2011-02-21
    • 1970-01-01
    • 2012-08-13
    • 2016-11-21
    • 2012-12-29
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多