首页 > 解决方案 > 为什么使用 junit-jupiter (聚合器)执行测试而不使用单独的依赖项?

问题描述

我正在使用 Maven 构建一个 Spring Boot 项目。

当我像这样设置 Junit jupiter 依赖项时

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

我得到了错误

七月 2019 年 2 月 23 日晚上 10:47:42 org.junit.platform.launcher.core.DefaultLauncher handleThrowable 警告:ID 为“junit-jupiter”的 TestEngine 未能发现测试 java.lang.NoClassDefFoundError:org/junit/platform/engine/support /发现/选择器解析器

但是当我使用聚合器设置依赖项时,测试会正常执行。

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.5.1</version>
    <scope>test</scope>
</dependency>

那么这两者有什么区别呢?

标签: javamavenjunitjunit5

解决方案


您缺少junit-platform-launcher工件。请参阅官方 JUnit 5 用户指南

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

您应该将此添加到您的pom.xml

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.5.2</version>
    <scope>test</scope>
</dependency>

推荐阅读