问题
每当我运行我的项目 JUnit 测试(使用 JUnit 5 和 Java 9 和 Eclipse Oxygen 1.a)时,我都会遇到 eclipse 找不到任何测试的问题。
说明
在运行配置下,eclipse甚至找不到用@Test注释的方法,而是只显示“(所有方法)”。下图希望能更好地了解我的设置:
https://i.stack.imgur.com/wVTPt.png
控制台输出:
java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:31)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.base/java.lang.Class.newInstance(Unknown Source)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:368)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:363)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:307)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:222)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.core.LauncherFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 11 more
到目前为止我尝试了什么
我已经试过了
从构建路径中删除测试文件夹并再次添加它。
将鼠标悬停在使用 @Test 注释的方法上开始测试,然后单击“Run as JUnit Test”。
从 Buildpath 中删除 JUnit 并再次添加
重启日食
我还将项目整个项目从一台机器移动到另一台机器,并在那里使用提供的 eclipse 安装进行了尝试
重命名测试方法。
重新输入 @Test 注释
其中一些步骤can be found here,但最终问题仍然存在。
我通过右键单击测试并选择“运行配置”并将“测试运行器:”选择更改为“JUnit 4”来解决此问题,如下所示:
https://i.stack.imgur.com/oNPRd.png
我再次进行了测试,它成功了。
我对 STS 3.9.1 有同样的问题。这似乎是一个 Eclipse 错误,但是,要修复此问题,您可以将测试依赖项 junit-platform-launcher
添加到您的项目 (https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher)
这就是我为使用 gradle 的项目所做的:
dependencies {
// other stuff here
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: "5.${junit5MinorVersion}"
testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: "1.${junit5MinorVersion}"
}
gradle.properties 文件:
junit5MinorVersion=1.0
如果您在使用 IntelliJ IDEA 时看到此异常,我相信同样适用。
build.gradle
。如果您使用的是 maven,它是 pom.xml
。 maven 的格式虽然不同。
就我而言,问题出在我自己身上,没有像 Eclipse 这样的 IDE。我已经导入了 JUnit 4 Test 类。
所以不要导入这个:
import org.junit.Test // JUnit 4
但是请导入那个:
import org.junit.jupiter.api.Test // JUnit 5
简单修复:(添加 JUnit 5 库)
指示:
右键单击项目 -> 构建路径 -> 配置构建路径
在弹出窗口中 -> 添加库 -> JUnit -> JUnit 5 -> 完成 -> 应用
您应该会看到 JUnit 5 库(及其 jar)已添加到您的项目中
右键单击项目 -> Maven -> 更新项目 -> 确定
您遇到了已修复的 Eclipse bug 525948,它将在 2018 年 3 月 21 日即将发布的 Oxygen.3 (4.7.3) 版本中发布。
作为解决方法,将您的测试代码放在单独的项目中,并将被测项目添加到模块路径中,但不要将 module-info.java
添加到您的测试项目中。使用您的项目、类和模块命名,它应该如下所示:
https://i.stack.imgur.com/NBXME.png
另请参阅my video that shows Java 9 and JUnit 5 in Eclipse Oxygen.1a in action
module-info.java
)。在 Eclipse 中,Java 项目只有一个输出文件夹,因此它只能包含一个或不包含 module-info.java
。但是即使项目不是模块化的(没有 module-info.java
),您也可以将模块添加到项目的模块路径中。
到目前为止的答案并没有解决与不一定使用 Eclipse 的其他人共享代码的问题。这是一个提议。关键是使用maven profile来解决Eclipse Case。
它假设您在 pom 中定义了一个属性 junit5.version
,例如:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit5.version>5.1.1</junit5.version>
</properties>
然后在 profiles
部分添加以下内容:
<profiles>
<profile>
<id>eclipse</id>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
</profiles>
之后您要做的就是在本地 Eclipse 中选择配置文件:右键单击您的项目并选择 Maven > Select Maven Profiles...
(或点击 Ctrl + Alt + P),然后检查我们刚刚创建的“eclipse”配置文件。
https://i.stack.imgur.com/9PFFU.png
这样你就完成了。您的 Eclipse 将按预期运行 Junit 5 测试,但您添加的配置不会污染其他构建或其他 IDE
使用 JUnit Jupiter (v.5.5.1) 添加这个 maven 依赖项解决了这个问题。
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
仅供参考,“没有使用 junit5 找到测试”的另一个原因是(无意或有意)将测试用例声明为“私有”:
// Example of test case that doesn't get included
@Test
private void testSomeMethod() {
}
他们需要公开。
没有一个解决方案有帮助:
问题是 Eclipse 2018-12 支持 JUnit 5.3.1。如果您使用之前的版本启动它,它将失败。
因此,请确保您至少使用 5.3.1。
5.4.0 也可以。
如果您的父 pom 是 Spring Boot,您需要确保(在依赖项管理中)将 junit-jupiter-api 设置为相同的版本。您不需要 junit-platform-runner 或 -launcher!
<junit-jupiter.version>5.4.1</junit-jupiter.version>
以覆盖 Spring boot (2.1.3.RELEASE) 中使用的版本。
Andrii Karaivanskyi's answer 之后是 Maven POM 声明:
<properties>
...
<junit-jupiter.version>5.2.0</junit-jupiter.version>
<junit-platform.version>1.2.0</junit-platform.version>
...
</properties>
<dependencies>
...
<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>
...
</dependencies>
更新
根据 Alexander Wessel 的评论,您可以使用 his answer 中所述的 org.junit:junit-bom 来提问 Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory。
scope=import type=pom
在dependencyManagement 标记中添加对BOM 的依赖项,然后在测试范围内将依赖项添加到junit-platform-launcher 和junit-jupiter-engine,在依赖项部分中不提供版本 . BOM 将始终确保您在类路径中只有一个版本的 JUnit Platform 和 Jupiter,并且各个版本都是预期的版本(例如 5.2.0 和 1.2.0)。
我实际上使用的是 spring-tool-suite-4-4.5.1,当我想运行一个测试类时我遇到了这个错误。解决方案是添加到库中的“java构建路径”、“junit5”
https://i.stack.imgur.com/4Vnjm.png
我也遇到了同样的问题,你只需要添加库,Junit 库已经随 Eclipse 一起提供了,所以你只需要按照下面的操作
构建路径 > 配置构建路径 > 库 > 添加库 > JUnit > 下一步 > 完成
这个对我有用
从一开始,错误消息就告诉您找不到类:NoClassDefFoundError
这意味着到 junit 的 PATH 是问题所在。
按右键单击项目文件夹并选择属性或选择项目文件夹并按组合 cmd + i。从列表“Java 构建路径”中选择。选择“Libraries”选项卡 如果将 JUnit 5(或 JUnit 4)添加到“Modulepath”,请选择“JUnit 5”并按 Remove。选择“类路径”,按“添加库...”。从打开的“添加库”窗口中,选择 JUnit,然后按下一步。选择您需要的 JUnit 库版本,然后按 Finish。
就这些。尝试再次运行测试。
代替:
import org.junit.Test;
和:
import org.junit.jupiter.api.Test;
创建新的TestCase 后我遇到了同样的问题:Eclipse -> New -> JUnit Test Case。它创建一个没有访问级别修饰符的类。我可以通过在 class 关键字之前放一个 public 来解决这个问题。
正如每个人都告知这是 IDE 错误,我在 Eclipse
和 STS
中进行了尝试。在这两种情况下,它都失败了。
作为一种解决方法,我通过修改 pom.xml 文件来修复,如下所示。
我添加了这两个 Maven 依赖项 junit-jupiter-engine
和 junit-platform-launcher
。
pom.xml
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform launcher -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
另外请确保在属性标签中添加两个 Maven 依赖项的版本。
<properties>
<java.version>1.8</java.version>
<junit-jupiter.version>5.2.0</junit-jupiter.version>
<junit-platform.version>1.2.0</junit-platform.version>
</properties>
你应该知道 :
@Before from junit4 与 @Test 一起使用:“import org.junit.Test”
和
来自 Junit5 的 @BeforeEach 带有:“import org.junit.jupiter.api.Test”
所以请确保您使用的是来自同一版本的 Junit 的导入,否则我猜它不会工作。
@Test 注解必须从 org.junit.jupiter.api.Test 导入,这样 Junit5 才能读取它。 Junit4 使用从 org.junit.Test 包导入的 @Test 注解。
使用 STS 3.9.1 我遇到了同样的问题。但是,目前我不需要任何新的 JUnit5 功能,所以我尝试强制使用旧版本。如果使用 maven,您可以在 pom.xml 中添加以下依赖项:
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.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.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
这对我有用(至少只要我不需要明确地需要 JUnit5)。
我正在使用 Eclipse 2019-09。
我必须将 junit-bom 版本更新到至少 5.4.0。我以前有 5.3.1,这导致了 OP 的相同症状。
我的配置现在是:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.5.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
您缺少带有组的 JUnit 5 平台启动器:'org.junit.platform',名称:'junit-platform-launcher'
只需添加您的 POM:
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>
由于无法将代码块发布到 comments,这是我在需要 JUnit 5 的项目中使用的 POM 模板。这允许在 Eclipse 中构建和“作为 JUnit 测试运行”,并使用普通 Maven 构建项目。
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>group</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>project name</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.3.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!-- only required when using parameterized tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
您可以看到,如果您想更新 JUnit,现在只需在一处更新版本。此外,平台版本号不需要出现在 POM 中的任何位置(在兼容版本中),它是通过 junit-bom
导入自动管理的。
您只能使用 junit-jupiter
而不是 junit-jupiter-api
、junit-platform-launcher
、junit-jupiter-engine
作为测试依赖项。
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
我在用着:
Spring Tool Suite 4 版本:4.4.2.RELEASE 构建 ID:201911201053 Java 版本:1.8.0_191
并且显示的消息是 No tests found with test runner 'JUnit5'
对我有用的是下面的配置
<properties>
<java.version>1.8</java.version>
<junit-jupiter.version>5.5.2</junit-jupiter.version>
<junit-platform.version>1.5.2</junit-platform.version>
</properties>
<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>
您可能正在从 org.junit.Test
导入 @Test
,这是一个 JUnit 4 注释。 Junit5 测试运行程序不会发现它。
Junit5 测试运行器将发现一个带有 org.junit.jupiter.api.Test
注释的测试
从 Import org.junit.Test throws error as "No Test found with Test Runner "JUnit 5"" 找到答案
您应该确保您的测试用例函数是公共的而不是私有的,以便让 Test Runner 可以访问它。
@Test
private void testmethod(){}
至
@Test
public void testmethod(){}
我在 Eclipse 版本 Oxygen.3a Release (4.7.3a) 中遇到了同样的错误。 Maven Dependencies 不匹配存在问题。为了解决,我已经用以下依赖项更新了我的 Pom.xml。
http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0 com.netapp.junitnmactiopractice JunitAndMactioPractice 0.0.1-SNAPSHOT
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.jupiter.version>5.1.1</junit.jupiter.version>
<junit.platform.version>1.1.1</junit.platform.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
junit-platform-runner
: "Runner for executing tests and test suites on the JUnit Platform in a JUnit 4 environment." 的 JUnit 5 用户指南
你应该改变
@Test
public static void testmethod(){}
至
@Test
public void testmethod(){}
@Test 不支持静态方法
对我来说,我配置了构建路径以添加 JUnit 5 库并添加依赖项
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
分开。
我也遇到了 Eclipse(版本:2019-12 (4.14.0))的这个问题。该解决方案似乎要么使用 IntelliJ,要么使用 Maven 测试在 Eclipse 中运行此类测试。