Java Eclipse 没有发现使用 JUnit 5 的测试,这是由 LauncherFactory 的 NoClassDefFoundError 引起的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46717693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:27:34  来源:igfitidea点击:

Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory

javaeclipsejunitjava-9junit5

提问by ShadowDragon

The problem

问题

Whenever I run my projects JUnit test (using JUnit 5 with Java 9 and Eclipse Oxygen 1.a) I encounter the problem that eclipse can't find any tests.

每当我运行我的项目 JUnit 测试(使用 Java 9 和 Eclipse Oxygen 1.a 的 JUnit 5)时,我都会遇到 eclipse 找不到任何测试的问题。

The description

说明

Under the run configuration, eclipse can't even find the method which is annotated with @Test, but instead only shows me "(all methods)". The following picture hopefully gives a better glimps of my setup:

在运行配置下,eclipse甚至找不到用@Test注解的方法,而是只显示我“ (所有方法)”。下图希望能更好地了解我的设置:

https://imgur.com/a/FTe9c

https://imgur.com/a/FTe9c

Console output:

控制台输出:

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

What I tried so far

到目前为止我尝试过的

I've already tried

我已经试过了

  • to remove the test folder from build path and add it again.
  • to start the test with hovering over the method annotated with @Test and then click "Run as JUnit Test".
  • remove JUnit from Buildpath and add it again
  • restart eclipse
  • I've also moved the project whole project from one machine to another machine and tried it with the provided eclipse installation there
  • to rename the test method.
  • to retype the @Test annotation
  • 从构建路径中删除测试文件夹并再次添加。
  • 将鼠标悬停在用@Test 注释的方法上开始测试,然后单击“作为 JUnit 测试运行”。
  • 从 Buildpath 中删除 JUnit 并再次添加
  • 重启日食
  • 我还将整个项目从一台机器移到另一台机器上,并在那里使用提供的 eclipse 安装进行了尝试
  • 重命名测试方法。
  • 重新输入@Test 注释

Some of these steps can be found here, but in the end the problem remained.

其中一些步骤可以在这里找到,但最终问题仍然存在。

采纳答案by howlger

You ran into Eclipse bug 525948which has already been fixed and which will be published in the upcoming release Oxygen.3 (4.7.3), March 21, 2018.

您遇到了Eclipse 错误 525948,该错误已得到修复,并将在 2018 年 3 月 21 日即将发布的 Oxygen.3 (4.7.3) 版本中发布。

As workaround, put your test code in a separate projectand add the project under test to the modulepath, but do not add a module-info.javato your test project. With your project, class and module naming, it should look something like this:

作为解决方法,将您的测试代码放在一个单独的项目中,并将被测项目添加到模块路径中,但不要将 a 添加module-info.java到您的测试项目中。使用您的项目、类和模块命名,它应该如下所示:

enter image description here

在此处输入图片说明

See also my video that shows Java 9 and JUnit 5 in Eclipse Oxygen.1a in action

另请参阅我的视频,该视频展示了 Eclipse Oxygen.1a 中的 Java 9 和 JUnit 5 的运行情况

回答by Andrii Karaivanskyi

I have the same issue with STS 3.9.1. It seems like an Eclipse bug, however, to fix this you can add a test dependency junit-platform-launcherto your project (https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher)

我对 STS 3.9.1 有同样的问题。这似乎是一个 Eclipse 错误,但是,要解决此问题,您可以junit-platform-launcher向项目添加测试依赖项(https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher

This is how I did for my project which uses gradle:

这就是我为使用 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 file:

gradle.properties 文件:

junit5MinorVersion=1.0

I believe the same applies if you see this exception while using IntelliJ IDEA.

如果您在使用 IntelliJ IDEA 时看到此异常,我相信同样适用。

回答by mahe

Using STS 3.9.1 I got the same problem. However, currently I do not require any new JUnit5 features, so I tried to force using an older version. If using maven, you can add following dependencies to your pom.xml:

使用 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>

This did the trick for me (at least as long as I do not need JUnit5 explicitly).

这对我有用(至少只要我不需要明确的 JUnit5)。

回答by du-it

I got the same problem after creating a new TestCase: Eclipse -> New -> JUnit Test Case. It creates a class without access level modifier. I could solve the problem by just putting a publicbefore the classkeyword.

创建新的测试用例后,我遇到了同样的问题:Eclipse -> 新建 -> JUnit 测试用例。它创建一个没有访问级别修饰符的类。我可以通过在class关键字之前放置一个public来解决这个问题。

回答by Aldian

Answers so far did not adress the problem of sharing code with other people who don't necessarily use Eclipse. Here is one proposition. The key is to use a maven profile to solve the Eclipse Case.

到目前为止的答案没有解决与不一定使用 Eclipse 的其他人共享代码的问题。这是一个提议。关键是使用maven profile来解决Eclipse Case。

It assumes you have defined a property junit5.versionin your pom like:

它假设您已经junit5.version在 pom 中定义了一个属性,例如:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit5.version>5.1.1</junit5.version>
</properties>

then in the profilessection add the following:

然后在该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>

All you have to do after this is to select the profile in your local Eclipse: Right click on your project and select Maven > Select Maven Profiles...(or hit Ctrl+ Alt+ P), and then check the "eclipse" profile we just created.

在此之后您要做的就是在本地 Eclipse 中选择配置文件:右键单击您的项目并选择Maven > Select Maven Profiles...(或点击Ctrl+ Alt+ P),然后检查我们刚刚创建的“eclipse”配置文件。

Selection Of Maven Profile

Maven配置文件的选择

With that you are done. Your Eclipse will run Junit 5 tests as expected, but the configuration you added won't pollute other builds or other IDE

这样你就完成了。您的 Eclipse 将按预期运行 Junit 5 测试,但您添加的配置不会污染其他构建或其他 IDE

回答by stringVector

I fixed the issue by right clicking the test and selecting 'Run Configurations' and changing the "Test runner:" selection to 'JUnit 4' as shown here:

我通过右键单击测试并选择“运行配置”并将“测试运行器:”选择更改为“JUnit 4”来解决该问题,如下所示:

Screenshot of run configruations

运行配置的屏幕截图

I ran the test again and it worked.

我再次运行了测试,它奏效了。

回答by Mike B

FYI, another cause of "No tests found using junit5" is (inadvertently or intentionally) declaring the test cases "private":

仅供参考,“未发现使用 junit5 的测试”的另一个原因是(无意或有意地)将测试用例声明为“私有”:

// Example of test case that doesn't get included
@Test
private void testSomeMethod() {
}

They need to be public.

他们需要公开。

回答by Harpreet Singh

Same error i faced in eclipse version Oxygen.3a Release (4.7.3a) . There is issue in Maven Dependencies mismatch.To solve i have updated my Pom.xml with following dependecies.

我在 Eclipse 版本 Oxygen.3a Release (4.7.3a) 中遇到了同样的错误。Maven 依赖项不匹配存在问题。为了解决我已经用以下依赖项更新了我的 Pom.xml。

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.netapp.junitnmactiopractice JunitAndMactioPractice 0.0.1-SNAPSHOT

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>

回答by GeroldBroser reinstates Monica

Following Andrii Karaivanskyi's answerhere's the Maven POM declaration:

Andrii Karaivanskyi 的回答之后,这里是 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>

UPDATE

更新

As per comment by Alexander Wesselyou can use org.junit:junit-bomas described in his answerto question Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory.

根据Alexander Wessel 的评论,您可以使用org.junit:junit-bom,如对问题Eclipse No tests found using JUnit 5引起的 NoClassDefFoundError for LauncherFactory 的回答中所述。

回答by Alexander Wessel

Since it's not possible to post code blocks into commentshere's the POM template I am using in projects requiring JUnit 5. This allows to build and "Run as JUnit Test" in Eclipse and building the project with plain Maven.

由于不可能将代码块发布到注释中,这是我在需要 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>

You can see that now you only have to update the version in one place if you want to update JUnit. Also the platform version number does not need to appear (in a compatible version) anywhere in your POM, it's automatically managed via the junit-bomimport.

您可以看到,如果您想更新 JUnit,现在只需在一处更新版本。此外,平台版本号不需要出现在 POM 中的任何地方(在兼容版本中),它是通过junit-bom导入自动管理的。