Eclipse No tests found using JUnit 5 导致 LauncherFactory 的 NoClassDefFoundError
Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory
问题
每当我 运行 我的项目 JUnit 测试(将 JUnit 5 与 Java 9 和 Eclipse Oxygen 1.a 结合使用)时,我都会遇到 Eclipse 找不到任何测试的问题。
描述
在运行配置下,eclipse连@Test注解的方法都找不到,只显示“(all methods)” .
希望下图能更好地展示我的设置:
控制台输出:
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
- 我还将项目整个项目从一台机器移动到另一台机器,并在那里使用提供的 eclipse 安装进行了尝试
- 重命名测试方法。
- 重新键入@Test 注释
其中一些步骤can be found here,但最后问题依旧。
您 运行 进入 Eclipse bug 525948,该问题已经修复,将在 2018 年 3 月 21 日即将发布的 Oxygen.3 (4.7.3) 版本中发布。
作为解决方法,将您的测试代码放在一个单独的项目中并将被测项目添加到模块路径,但不要向您的测试项目添加 module-info.java
.对于你的项目,class 和模块命名,它应该看起来像这样:
另见 my video that shows Java 9 and JUnit 5 in Eclipse Oxygen.1a in action
我对 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 时看到此异常,我相信同样适用。
使用 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 -> 新建 -> JUnit 测试用例。它创建一个没有访问级别修饰符的 class。我可以通过在 class 关键字前添加 public 来解决问题。
到目前为止的答案没有解决与不一定使用 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>
之后您要做的就是 select 本地 Eclipse 中的配置文件:右键单击您的项目并 select Maven > Select Maven Profiles...
(或点击 Ctrl + Alt + P), 然后查看我们刚刚创建的"eclipse"配置文件
这样你就完成了。您的 Eclipse 将按预期 运行 Junit 5 测试,但您添加的配置不会污染其他构建或其他 IDE
我通过右键单击测试并选择 'Run Configurations' 并将 "Test runner:" 选择更改为 'JUnit 4' 来解决此问题,如下所示:
我 运行 再次测试并且成功了。
仅供参考,"No tests found using junit5" 的另一个原因是(无意或有意)声明测试用例 "private":
// Example of test case that doesn't get included
@Test
private void testSomeMethod() {
}
他们需要 public。
我在 eclipse 版本 Oxygen.3a Release (4.7.3a) 中遇到了同样的错误。 Maven Dependencies mismatch.To 中存在问题解决我已经用以下依赖项更新了我的 Pom.xml。
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.netapp.junitnmactiopractice
Junit和Mactio实践
0.0.1-快照
<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>
之后是 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 you can use org.junit:junit-bom as described in to question 的评论。
因为不可能 post 代码块进入 这里是我在需要 JUnit 5 的项目中使用的 POM 模板。这允许在 Eclipse 中构建和 "Run as JUnit Test" 并构建使用普通 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
导入自动管理。
由于每个人都告知这是 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>
None 个解决方案有帮助:
问题是 Eclipse 2018-12 支持 JUnit 5.3.1。如果您使用之前的版本启动它,它将失败。
因此请确保您至少使用 5.3.1。
5.4.0 也可以。
如果你的父 pom 是 Spring Boot,你需要确保(在依赖管理中)设置了 junit-jupiter-api到同一个版本。你不需要 junit-platform-runner 或 -launcher!
就我而言,问题出在我自己身上,与 Eclipse 不同 IDE。我已经导入了 JUnit 4 测试 class.
所以不要导入这个:
import org.junit.Test // JUnit 4
但一定要导入那个:
import org.junit.jupiter.api.Test // JUnit 5
在您的 POM 中添加:
<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>
在 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>
从一开始,错误消息告诉您未找到 class:NoClassDefFoundError
,这意味着 junit 的路径存在问题。
右键单击项目文件夹并选择属性或 select 项目文件夹并按组合键 cmd + i。
select 来自列表 "Java Build Path".
- select "Libraries" 选项卡
- 如果 JUnit 5(或 JUnit 4)添加到 "Modulepath",select "JUnit 5",然后按删除。
- select"Classpath",按"Add Library..."。
- 从打开的"Add Library"window,selectJUnit,按下一步。
- Select 您需要的 JUnit 库版本,然后按完成。
就是这样。再次尝试 运行 测试。
我正在使用 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-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>
你应该改变
@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>
分开。
我 运行 遇到了同样的错误,在我的情况下,只需 Project Properties > Maven > Update project
and/or 清理和重建项目即可。
我正在使用:
Spring 工具套件 4
版本:4.4.2.RELEASE
内部版本号:201911201053
Java版本:1.8.0_191
并且显示的消息是
未找到测试运行程序 '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>
刚刚通过 Eclipse 菜单添加了我的 Test.class 并且工作正常。
右击项目 -> 新建 -> 其他 -> JUnit -> JUnit 测试用例
我在使用 Eclipse(版本:2019-12 (4.14.0))时也遇到了这个问题。该解决方案似乎要么使用 IntelliJ,要么使用 Maven 测试 运行 Eclipse 中的此类测试。
可能是junit-platform-launcher / junit-platform-runner版本问题
1.1.0 不工作
1.4.1 在我的设置中有效。
我认为这是 eclipse 中的一个错误,因为它使用的是更高版本的 junit 库,而不是其他版本。
这解决了我的问题。对我来说,另一项决议似乎不太可行或风险不大。
谢谢。
当我将 jupiter api 版本更改为最新版本时,问题已解决。同时,我的 eclipse 和其他 eclipse 扩展 ide(例如 STS)出现构建路径错误。对于每个 Maven 更新,ide 强制我设置 JRE 系统库。
我实际上使用 spring-tool-suite-4-4.5.1,当我想要 运行 测试 class 时遇到了这个错误。
解决方案是添加到库
中的 'java build path'、'junit5'
你应该知道:
@Before 来自 junit4 与 @Test 一起使用:“import org.junit.Test
和
@BeforeEach 来自 Junit5 与:“import org.junit.jupiter.api.Test”
所以请确保您使用的是来自相同版本 Junit 的导入,否则我猜它不会工作。
您缺少带有组的 JUnit 5 平台启动器:'org.junit.platform',名称:'junit-platform-launcher'
只需在您的 POM 中添加:
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>
简单修复:(添加 JUnit 5 库)
说明:
- 右键单击项目 -> 构建路径 -> 配置构建路径
- 在弹窗->添加库->JUnit->JUnit 5->完成->应用
- 您应该会看到 JUnit 5 库(及其 jar)已添加到您的项目中
- 右键单击项目 -> Maven -> 更新项目 -> 确定
我复制了一些代码并创建了一个 TC。基本上将普通的 java class 转换为 JUnit TC。
我是 gettgig 错误。
后来我从 New -> Junit TC 创建了一个新的 JUnit Class。复制相同的代码。它工作正常并且错误消失了。
我也遇到了同样的问题,你只需要添加库,Junit 库已经随 Eclipse 一起提供,所以你只需要按照下面的操作
构建路径 > 配置构建路径 > 库 > 添加库 > JUnit > 下一步 > 完成
对我有用
使用 spring 启动 api 实现 Mockito 时会发生此问题。下面在 gradle 文件
中添加以下依赖项
compile group: 'org.mockito', name: 'mockito-core', version: '2.22.0'
注:Spring引导版本2.2.0
尝试将您的单元测试配置重新配置为项目 JRE。
我遇到了类似的问题。这是由于属性中的源路径不正确。
打开项目属性 -> Java Build Path -> Sources。如果是你的情况,请删除不正确的路径
将 org.junit.platform 添加到您的 pom 并构建它。接下来,您需要转到测试文件的“运行 配置”并在类路径中添加 JUNIT,APPLY->运行 将解决问题。
我在 SpringBoot 项目中遇到了同样的问题,就我而言,原因是输出文件夹“/target/test-classes”是空的。
在.classpath文件中手动修复,寻找属性为“test”的classpathentry并更改输出属性的内容:
来自这里:
<classpathentry kind="src" output="<project folder>/target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
对此:
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
您可能从 org.junit.Test
导入 @Test
,这是一个 JUnit 4 注释。 Junit5 测试运行器不会发现它。
Junit5 测试运行器将发现一个用 org.junit.jupiter.api.Test
注释的测试
从
找到答案
删除 JUnit 库,然后再次添加它为我解决了这个问题。
替换:
import org.junit.Test;
与:
import org.junit.jupiter.api.Test;
您应该确保您的测试用例函数是 public 而不是私有的,以便测试运行程序可以访问它。
@Test
private void testmethod(){}
至
@Test
public void testmethod(){}
将 Java 项目转换为 Maven 项目解决了我的问题。通过以下方式在 Eclipse 上完成转换:右键单击项目 -> 配置 -> 转换为 Maven 项目
@Test 注释必须从 org.junit.jupiter.api.Test 导入,以便 Junit5 可以读取它。
Junit4 使用从 org.junit.Test 包导入的 @Test 注解。
我痛苦地一一检查了现有的答案,其中 none 个适用于我,none 个为我修复了它。
就我而言,在“项目属性”和“源”选项卡下,我已将测试文件夹添加为源路径。此条目下有一个子属性显示 Output Folder
。我将其设置为我希望生成测试 class 文件的位置,并将它们保存在该位置。
但是,当 运行 实际上在寻找 不同于我指定的文件夹,这是target/test-classes
。如果我手动将我的 class 文件从我指定的 Output Folder
文件夹移动到 target/test-classes
文件夹,那么我的单元测试将 运行.
因此请确保您的 class 文件位于 运行 时间认为它们应该位于的位置。
将其更改为 JUnit 4 及其作品。请尝试一下(如果你有简单的例子,那么一定要试试这个。)点击那个 class 文件 --> 它会出现 Run/Debug 设置 ---> 点击新建 --> Select JUnit --> 单击确定 --> 将 TestRunner 更改为 Junit 4。然后单击应用并确定。然后 运行 一定会成功的。
如果您使用 beforeAll、beforeEach 方法,请删除它们,然后再 运行。如果你没有得到任何错误,你可以改变你的方法静态情况。
在我的例子中,我之前使用的是没有静态密钥的 beforeAll,然后我使用静态密钥进行方法签名,我的问题就解决了。
问题
每当我 运行 我的项目 JUnit 测试(将 JUnit 5 与 Java 9 和 Eclipse Oxygen 1.a 结合使用)时,我都会遇到 Eclipse 找不到任何测试的问题。
描述
在运行配置下,eclipse连@Test注解的方法都找不到,只显示“(all methods)” . 希望下图能更好地展示我的设置:
控制台输出:
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
- 我还将项目整个项目从一台机器移动到另一台机器,并在那里使用提供的 eclipse 安装进行了尝试
- 重命名测试方法。
- 重新键入@Test 注释
其中一些步骤can be found here,但最后问题依旧。
您 运行 进入 Eclipse bug 525948,该问题已经修复,将在 2018 年 3 月 21 日即将发布的 Oxygen.3 (4.7.3) 版本中发布。
作为解决方法,将您的测试代码放在一个单独的项目中并将被测项目添加到模块路径,但不要向您的测试项目添加 module-info.java
.对于你的项目,class 和模块命名,它应该看起来像这样:
另见 my video that shows Java 9 and JUnit 5 in Eclipse Oxygen.1a in action
我对 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 时看到此异常,我相信同样适用。
使用 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 -> 新建 -> JUnit 测试用例。它创建一个没有访问级别修饰符的 class。我可以通过在 class 关键字前添加 public 来解决问题。
到目前为止的答案没有解决与不一定使用 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>
之后您要做的就是 select 本地 Eclipse 中的配置文件:右键单击您的项目并 select Maven > Select Maven Profiles...
(或点击 Ctrl + Alt + P), 然后查看我们刚刚创建的"eclipse"配置文件
这样你就完成了。您的 Eclipse 将按预期 运行 Junit 5 测试,但您添加的配置不会污染其他构建或其他 IDE
我通过右键单击测试并选择 'Run Configurations' 并将 "Test runner:" 选择更改为 'JUnit 4' 来解决此问题,如下所示:
我 运行 再次测试并且成功了。
仅供参考,"No tests found using junit5" 的另一个原因是(无意或有意)声明测试用例 "private":
// Example of test case that doesn't get included
@Test
private void testSomeMethod() {
}
他们需要 public。
我在 eclipse 版本 Oxygen.3a Release (4.7.3a) 中遇到了同样的错误。 Maven Dependencies mismatch.To 中存在问题解决我已经用以下依赖项更新了我的 Pom.xml。
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.netapp.junitnmactiopractice Junit和Mactio实践 0.0.1-快照
<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>
<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 you can use org.junit:junit-bom as described in
因为不可能 post 代码块进入
<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
导入自动管理。
由于每个人都告知这是 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>
None 个解决方案有帮助:
问题是 Eclipse 2018-12 支持 JUnit 5.3.1。如果您使用之前的版本启动它,它将失败。
因此请确保您至少使用 5.3.1。
5.4.0 也可以。
如果你的父 pom 是 Spring Boot,你需要确保(在依赖管理中)设置了 junit-jupiter-api到同一个版本。你不需要 junit-platform-runner 或 -launcher!
就我而言,问题出在我自己身上,与 Eclipse 不同 IDE。我已经导入了 JUnit 4 测试 class.
所以不要导入这个:
import org.junit.Test // JUnit 4
但一定要导入那个:
import org.junit.jupiter.api.Test // JUnit 5
在您的 POM 中添加:
<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>
在 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>
从一开始,错误消息告诉您未找到 class:NoClassDefFoundError
,这意味着 junit 的路径存在问题。
右键单击项目文件夹并选择属性或 select 项目文件夹并按组合键 cmd + i。
select 来自列表 "Java Build Path".
- select "Libraries" 选项卡
- 如果 JUnit 5(或 JUnit 4)添加到 "Modulepath",select "JUnit 5",然后按删除。
- select"Classpath",按"Add Library..."。
- 从打开的"Add Library"window,selectJUnit,按下一步。
- Select 您需要的 JUnit 库版本,然后按完成。
就是这样。再次尝试 运行 测试。
我正在使用 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-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>
你应该改变
@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>
分开。
我 运行 遇到了同样的错误,在我的情况下,只需 Project Properties > Maven > Update project
and/or 清理和重建项目即可。
我正在使用:
Spring 工具套件 4 版本:4.4.2.RELEASE 内部版本号:201911201053 Java版本:1.8.0_191
并且显示的消息是 未找到测试运行程序 '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>
刚刚通过 Eclipse 菜单添加了我的 Test.class 并且工作正常。 右击项目 -> 新建 -> 其他 -> JUnit -> JUnit 测试用例
我在使用 Eclipse(版本:2019-12 (4.14.0))时也遇到了这个问题。该解决方案似乎要么使用 IntelliJ,要么使用 Maven 测试 运行 Eclipse 中的此类测试。
可能是junit-platform-launcher / junit-platform-runner版本问题
1.1.0 不工作
1.4.1 在我的设置中有效。
我认为这是 eclipse 中的一个错误,因为它使用的是更高版本的 junit 库,而不是其他版本。
这解决了我的问题。对我来说,另一项决议似乎不太可行或风险不大。 谢谢。
当我将 jupiter api 版本更改为最新版本时,问题已解决。同时,我的 eclipse 和其他 eclipse 扩展 ide(例如 STS)出现构建路径错误。对于每个 Maven 更新,ide 强制我设置 JRE 系统库。
我实际上使用 spring-tool-suite-4-4.5.1,当我想要 运行 测试 class 时遇到了这个错误。 解决方案是添加到库
中的 'java build path'、'junit5'你应该知道:
@Before 来自 junit4 与 @Test 一起使用:“import org.junit.Test
和
@BeforeEach 来自 Junit5 与:“import org.junit.jupiter.api.Test”
所以请确保您使用的是来自相同版本 Junit 的导入,否则我猜它不会工作。
您缺少带有组的 JUnit 5 平台启动器:'org.junit.platform',名称:'junit-platform-launcher'
只需在您的 POM 中添加:
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>
简单修复:(添加 JUnit 5 库)
说明:
- 右键单击项目 -> 构建路径 -> 配置构建路径
- 在弹窗->添加库->JUnit->JUnit 5->完成->应用
- 您应该会看到 JUnit 5 库(及其 jar)已添加到您的项目中
- 右键单击项目 -> Maven -> 更新项目 -> 确定
我复制了一些代码并创建了一个 TC。基本上将普通的 java class 转换为 JUnit TC。 我是 gettgig 错误。 后来我从 New -> Junit TC 创建了一个新的 JUnit Class。复制相同的代码。它工作正常并且错误消失了。
我也遇到了同样的问题,你只需要添加库,Junit 库已经随 Eclipse 一起提供,所以你只需要按照下面的操作
构建路径 > 配置构建路径 > 库 > 添加库 > JUnit > 下一步 > 完成
对我有用
使用 spring 启动 api 实现 Mockito 时会发生此问题。下面在 gradle 文件
中添加以下依赖项compile group: 'org.mockito', name: 'mockito-core', version: '2.22.0'
注:Spring引导版本2.2.0
尝试将您的单元测试配置重新配置为项目 JRE。
我遇到了类似的问题。这是由于属性中的源路径不正确。 打开项目属性 -> Java Build Path -> Sources。如果是你的情况,请删除不正确的路径
将 org.junit.platform 添加到您的 pom 并构建它。接下来,您需要转到测试文件的“运行 配置”并在类路径中添加 JUNIT,APPLY->运行 将解决问题。
我在 SpringBoot 项目中遇到了同样的问题,就我而言,原因是输出文件夹“/target/test-classes”是空的。
在.classpath文件中手动修复,寻找属性为“test”的classpathentry并更改输出属性的内容:
来自这里:
<classpathentry kind="src" output="<project folder>/target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
对此:
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
您可能从 org.junit.Test
导入 @Test
,这是一个 JUnit 4 注释。 Junit5 测试运行器不会发现它。
Junit5 测试运行器将发现一个用 org.junit.jupiter.api.Test
从
删除 JUnit 库,然后再次添加它为我解决了这个问题。
替换:
import org.junit.Test;
与:
import org.junit.jupiter.api.Test;
您应该确保您的测试用例函数是 public 而不是私有的,以便测试运行程序可以访问它。
@Test
private void testmethod(){}
至
@Test
public void testmethod(){}
将 Java 项目转换为 Maven 项目解决了我的问题。通过以下方式在 Eclipse 上完成转换:右键单击项目 -> 配置 -> 转换为 Maven 项目
@Test 注释必须从 org.junit.jupiter.api.Test 导入,以便 Junit5 可以读取它。 Junit4 使用从 org.junit.Test 包导入的 @Test 注解。
我痛苦地一一检查了现有的答案,其中 none 个适用于我,none 个为我修复了它。
就我而言,在“项目属性”和“源”选项卡下,我已将测试文件夹添加为源路径。此条目下有一个子属性显示 Output Folder
。我将其设置为我希望生成测试 class 文件的位置,并将它们保存在该位置。
但是,当 运行 实际上在寻找 不同于我指定的文件夹,这是target/test-classes
。如果我手动将我的 class 文件从我指定的 Output Folder
文件夹移动到 target/test-classes
文件夹,那么我的单元测试将 运行.
因此请确保您的 class 文件位于 运行 时间认为它们应该位于的位置。
将其更改为 JUnit 4 及其作品。请尝试一下(如果你有简单的例子,那么一定要试试这个。)点击那个 class 文件 --> 它会出现 Run/Debug 设置 ---> 点击新建 --> Select JUnit --> 单击确定 --> 将 TestRunner 更改为 Junit 4。然后单击应用并确定。然后 运行 一定会成功的。
如果您使用 beforeAll、beforeEach 方法,请删除它们,然后再 运行。如果你没有得到任何错误,你可以改变你的方法静态情况。
在我的例子中,我之前使用的是没有静态密钥的 beforeAll,然后我使用静态密钥进行方法签名,我的问题就解决了。