使用 Maven 在 Eclipse 中为 JUnit 5 配置类路径

Configure classpath in eclipse for JUnit 5 with Maven

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 的正确方法?

If I do the former, then everybody using eclipse has to do this himself.

我假设您打算通过右键单击 JUnit 测试 class 并选择 运行 as > JUnit Test[=25 来让 eclipse 用户有机会执行测试=].我不知道这是否是 正确的方法,但要这样做,除了 JUnit Jupiter API/Engine 之外,您还需要添加一个额外的依赖项,即 JUnit 平台启动器

例如:

<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,如果 [=18=18=]ClassNotFoundException =]缺少 JUnit 平台启动器。