IntelliJ JUnit 5测试可以作为Gradle任务正常运行。当作为单个测试运行时,有时会出现异常:NoClassDefFoundError ... / TestExecutionListener

时间:2018-10-22 20:08:08

标签: java gradle intellij-idea junit5

在Windows 10上使用IntelliJ IDEA版本2018.2.5(社区版),并学习基于JUnit团队基于parameterized tests的Gradle 4.8中的JUnit 5 JUnit 5 samples

该测试与gradle任务 test 一样运行,但有时在运行单个测试时会获得

Exception in thread "main" java.lang.NoClassDefFoundError:  
   org/junit/platform/launcher/TestExecutionListener

在仍然使用IntelliJ 2018.2.5(Community Edition),带有参数测试的JUnit 5和Gradle的情况下如何解决此异常?

我一直在试图找到答案,但是我遇到的大多数答案都表明,将IntellJ与JUnit 5结合使用正在过渡中,因此不确定该信任什么。

详细信息

我知道有比正常或超出正常水平更多的细节和屏幕截图,但是当您学习新东西并需要帮助时,开始寻找信息时,您会厌倦看到一些重要细节的答案。因此,对于那些在同一条船上并且需要看到缺少的细节的人,希望我已将其包括在这里。虽然可能不是您要寻求的问题,但此处的详细信息可能会对您有所帮助。

异常堆栈跟踪

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestExecutionListener
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:45)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.TestExecutionListener
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more

软件版本

Microsoft Windows版本10.0.171314.345

IntelliJ IDEA 2018.2.5(社区版)
版本#IC-182.4892.20,于2018年10月16日发布 JRE:1.8.0_152-release-1248-b19 amd64
JVM:JetBrains s.r.o
的OpenJDK 64位服务器VM
Project SDK:11(Java版本“ 11.0.1”) 对于此示例,IntelliJ是干净版本。

使用的特定JUnit示例:junit5-jupiter-starter-gradle

等级:4.8

build.gradle-与演示完全相同

plugins {
    id 'java'
    id 'eclipse' // optional (to generate Eclipse project files)
    id 'idea' // optional (to generate IntelliJ IDEA project files)
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
    testCompile('org.junit.jupiter:junit-jupiter-params:5.3.1')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

wrapper {
    gradleVersion = '4.8'
}

平跑者:Platform Test Runner

enter image description here

用于构建项目的步骤

下载了junit5-samples
未压缩的文件
在目录junit5-samples-r5.3.1下将junit5-jupiter-starter-gradle复制到C:\Users\Eric\IdeaProjects

在欢迎屏幕上使用IntelliJ 导入项目来加载项目。

enter image description here enter image description here

为“导入项目”对话框选项选择默认选项,例如单击所有Import Project对话框页面的“下一步”或“完成”。

C:.
|   .gitignore
|   build-JITPACK.gradle
|   build-SNAPSHOT.gradle
|   build.gradle
|   gradlew
|   gradlew.bat
|   README.md
|
+---.idea
|   |   misc.xml
|   |   modules.xml
|   |   workspace.xml
|   |
|   \---libraries
|           gradle_wrapper.xml
|
+---gradle
|   \---wrapper
|           gradle-wrapper.jar
|           gradle-wrapper.properties
|
\---src
    +---main
    |   |   main.iml
    |   |
    |   \---java
    |       \---com
    |           \---example
    |               \---project
    |                       Calculator.java
    |
    \---test
        |   test.iml
        |
        \---java
            \---com
                \---example
                    \---project
                            CalculatorTests.java

项目打开时,收到有关Unlinked Gradle project的事件日志,并单击了Import Gradle project

enter image description here enter image description here enter image description here

C:.
|   .gitignore
|   build-JITPACK.gradle
|   build-SNAPSHOT.gradle
|   build.gradle
|   gradlew
|   gradlew.bat
|   README.md
|
+---.gradle
|   +---4.8
|   |   +---fileChanges
|   |   |       last-build.bin
|   |   |
|   |   \---fileHashes
|   |           fileHashes.lock
|   |
|   \---vcsWorkingDirs
|           gc.properties
|
+---.idea
|   |   compiler.xml
|   |   gradle.xml
|   |   misc.xml
|   |   modules.xml
|   |   workspace.xml
|   |
|   +---libraries
|   |       gradle_wrapper.xml
|   |       Gradle__org_apiguardian_apiguardian_api_1_0_0.xml
|   |       Gradle__org_junit_jupiter_junit_jupiter_api_5_3_1.xml
|   |       Gradle__org_junit_jupiter_junit_jupiter_engine_5_3_1.xml
|   |       Gradle__org_junit_jupiter_junit_jupiter_params_5_3_1.xml
|   |       Gradle__org_junit_platform_junit_platform_commons_1_3_1.xml
|   |       Gradle__org_junit_platform_junit_platform_engine_1_3_1.xml
|   |       Gradle__org_opentest4j_opentest4j_1_1_1.xml
|   |
|   \---modules
|           junit5-jupiter-starter-gradle.iml
|           junit5-jupiter-starter-gradle_main.iml
|           junit5-jupiter-starter-gradle_test.iml
|
+---gradle
|   \---wrapper
|           gradle-wrapper.jar
|           gradle-wrapper.properties
|
\---src
    +---main
    |   |   main.iml
    |   |
    |   \---java
    |       \---com
    |           \---example
    |               \---project
    |                       Calculator.java
    |
    \---test
        |   test.iml
        |
        \---java
            \---com
                \---example
                    \---project
                            CalculatorTests.java

使用Gradle任务构建项目:构建

enter image description here

C:.
|   .gitignore
|   build-JITPACK.gradle
|   build-SNAPSHOT.gradle
|   build.gradle
|   gradlew
|   gradlew.bat
|   README.md
|
+---.gradle
|   +---4.8
|   |   +---fileChanges
|   |   |       last-build.bin
|   |   |
|   |   +---fileContent
|   |   |       annotation-processors.bin
|   |   |       fileContent.lock
|   |   |
|   |   +---fileHashes
|   |   |       fileHashes.bin
|   |   |       fileHashes.lock
|   |   |       resourceHashesCache.bin
|   |   |
|   |   \---taskHistory
|   |           taskHistory.bin
|   |           taskHistory.lock
|   |
|   +---buildOutputCleanup
|   |       buildOutputCleanup.lock
|   |       cache.properties
|   |       outputFiles.bin
|   |
|   \---vcsWorkingDirs
|           gc.properties
|
+---.idea
|   |   compiler.xml
|   |   gradle.xml
|   |   misc.xml
|   |   modules.xml
|   |   workspace.xml
|   |
|   +---libraries
|   |       gradle_wrapper.xml
|   |       Gradle__org_apiguardian_apiguardian_api_1_0_0.xml
|   |       Gradle__org_junit_jupiter_junit_jupiter_api_5_3_1.xml
|   |       Gradle__org_junit_jupiter_junit_jupiter_engine_5_3_1.xml
|   |       Gradle__org_junit_jupiter_junit_jupiter_params_5_3_1.xml
|   |       Gradle__org_junit_platform_junit_platform_commons_1_3_1.xml
|   |       Gradle__org_junit_platform_junit_platform_engine_1_3_1.xml
|   |       Gradle__org_opentest4j_opentest4j_1_1_1.xml
|   |
|   \---modules
|           junit5-jupiter-starter-gradle.iml
|           junit5-jupiter-starter-gradle_main.iml
|           junit5-jupiter-starter-gradle_test.iml
|
+---build
|   +---classes
|   |   \---java
|   |       +---main
|   |       |   \---com
|   |       |       \---example
|   |       |           \---project
|   |       |                   Calculator.class
|   |       |
|   |       \---test
|   |           \---com
|   |               \---example
|   |                   \---project
|   |                           CalculatorTests.class
|   |
|   +---libs
|   |       junit5-jupiter-starter-gradle.jar
|   |
|   +---reports
|   |   \---tests
|   |       \---test
|   |           |   index.html
|   |           |
|   |           +---classes
|   |           |       com.example.project.CalculatorTests.html
|   |           |
|   |           +---css
|   |           |       base-style.css
|   |           |       style.css
|   |           |
|   |           +---js
|   |           |       report.js
|   |           |
|   |           \---packages
|   |                   com.example.project.html
|   |
|   +---test-results
|   |   \---test
|   |       |   TEST-com.example.project.CalculatorTests.xml
|   |       |
|   |       \---binary
|   |               output.bin
|   |               output.bin.idx
|   |               results.bin
|   |
|   \---tmp
|       +---compileJava
|       +---compileTestJava
|       \---jar
|               MANIFEST.MF
|
+---gradle
|   \---wrapper
|           gradle-wrapper.jar
|           gradle-wrapper.properties
|
\---src
    +---main
    |   |   main.iml
    |   |
    |   \---java
    |       \---com
    |           \---example
    |               \---project
    |                       Calculator.java
    |
    \---test
        |   test.iml
        |
        \---java
            \---com
                \---example
                    \---project
                            CalculatorTests.java

Gradle缓存的文件

C:\Users\Eric\.gradle\caches>tree modules-2 /A /F

C:\USERS\ERIC\.GRADLE\CACHES\MODULES-2
|   modules-2.lock
|
+---files-2.1
|   +---org.apiguardian
|   |   \---apiguardian-api
|   |       \---1.0.0
|   |           +---2c4e5835b7580f2696be7ee1402f4309b3665cf4
|   |           |       apiguardian-api-1.0.0.pom
|   |           |
|   |           +---3ef5276905e36f4d8055fe3cb0bdcc7503ffc85d
|   |           |       apiguardian-api-1.0.0.jar
|   |           |
|   |           \---777508fa9f3e03cafb3c1fb2eba3dca317f4b1ee
|   |                   apiguardian-api-1.0.0-sources.jar
|   |
|   +---org.junit.jupiter
|   |   +---junit-jupiter-api
|   |   |   \---5.3.1
|   |   |       +---39e68334cdee95898567f54d7358a6926262dde6
|   |   |       |       junit-jupiter-api-5.3.1.pom
|   |   |       |
|   |   |       +---a7e97eac2784395cb991403f9641b042ad972941
|   |   |       |       junit-jupiter-api-5.3.1.jar
|   |   |       |
|   |   |       \---b03f607c0822a283c972e37eaeacb00dc3a3c2e7
|   |   |               junit-jupiter-api-5.3.1-sources.jar
|   |   |
|   |   +---junit-jupiter-engine
|   |   |   \---5.3.1
|   |   |       +---c769957170b2240854d6da6c6706dcc2a281542f
|   |   |       |       junit-jupiter-engine-5.3.1-sources.jar
|   |   |       |
|   |   |       +---e0b10da40cae904d69b805021bc1f5d64994aa93
|   |   |       |       junit-jupiter-engine-5.3.1.pom
|   |   |       |
|   |   |       \---e2676b1786c57a80eb98f5bebd51a3d05e228c40
|   |   |               junit-jupiter-engine-5.3.1.jar
|   |   |
|   |   \---junit-jupiter-params
|   |       \---5.3.1
|   |           +---9dfac3fbd6768974fc0c142304a3e90ba713b2a8
|   |           |       junit-jupiter-params-5.3.1.jar
|   |           |
|   |           +---bc4eb2ef307286fcd45551d4da397518c2e15394
|   |           |       junit-jupiter-params-5.3.1-sources.jar
|   |           |
|   |           \---cf0c573f1a0446afb8d544a5fd53dae4bb433b79
|   |                   junit-jupiter-params-5.3.1.pom
|   |
|   +---org.junit.platform
|   |   +---junit-platform-commons
|   |   |   \---1.3.1
|   |   |       +---297bb35bca3d229c5e6edbffd22de7b5ad4cf430
|   |   |       |       junit-platform-commons-1.3.1-sources.jar
|   |   |       |
|   |   |       +---67b7edddfac1935e6e6d9b58d7c7df6db59b1d39
|   |   |       |       junit-platform-commons-1.3.1.jar
|   |   |       |
|   |   |       \---c47714600308339d4b5e203974ebd2ce50eb2109
|   |   |               junit-platform-commons-1.3.1.pom
|   |   |
|   |   \---junit-platform-engine
|   |       \---1.3.1
|   |           +---3ee68a06bbdab157dd260e2095c356481d6cd172
|   |           |       junit-platform-engine-1.3.1.jar
|   |           |
|   |           +---db2bf7e793fec08aed5c588eb495595e4acc9fc7
|   |           |       junit-platform-engine-1.3.1.pom
|   |           |
|   |           \---effe038ffcf21f6988e78b12119ca456d7d144fd
|   |                   junit-platform-engine-1.3.1-sources.jar
|   |
|   \---org.opentest4j
|       \---opentest4j
|           \---1.1.1
|               +---6554a839d3c3f1c77c593498a3ae4b692878946a
|               |       opentest4j-1.1.1.pom
|               |
|               +---88a3a2cb15c413565462cea99f201b67bc6d2f10
|               |       opentest4j-1.1.1-sources.jar
|               |
|               \---efd9f971e91074491ea55b19f67b13470cf4fcdd
|                       opentest4j-1.1.1.jar
|
\---metadata-2.58
    |   module-artifact.bin
    |   module-artifacts.bin
    |   module-metadata.bin
    |   resource-at-url.bin
    |
    \---descriptors
        +---org.apiguardian
        |   \---apiguardian-api
        |       \---1.0.0
        |           \---f8e6315c37eb56998f7a5ba08e30db71
        |                   descriptor.bin
        |
        +---org.junit.jupiter
        |   +---junit-jupiter-api
        |   |   \---5.3.1
        |   |       \---f8e6315c37eb56998f7a5ba08e30db71
        |   |               descriptor.bin
        |   |
        |   +---junit-jupiter-engine
        |   |   \---5.3.1
        |   |       \---f8e6315c37eb56998f7a5ba08e30db71
        |   |               descriptor.bin
        |   |
        |   \---junit-jupiter-params
        |       \---5.3.1
        |           \---f8e6315c37eb56998f7a5ba08e30db71
        |                   descriptor.bin
        |
        +---org.junit.platform
        |   +---junit-platform-commons
        |   |   \---1.3.1
        |   |       \---f8e6315c37eb56998f7a5ba08e30db71
        |   |               descriptor.bin
        |   |
        |   \---junit-platform-engine
        |       \---1.3.1
        |           \---f8e6315c37eb56998f7a5ba08e30db71
        |                   descriptor.bin
        |
        \---org.opentest4j
            \---opentest4j
                \---1.1.1
                    \---f8e6315c37eb56998f7a5ba08e30db71
                            descriptor.bin

右侧的封闭面板
使用左侧的Project面板
展开目录以显示test/java/com.example.project/CalculatorTests.java
双击CalculatorTests.java

enter image description here

要解决第一个错误,请将光标放在第13行的Assertions上,然后按ALT+Enter
点击第一个选项

Add library 'Gradle: org.junit.jupiter:junit-jupiter-api:5.3.1' to classpath  

要解决第二个错误,请将光标放在第17行的ParameterizedTest上,然后按ALT+Enter
点击第一个选项

Add library 'Gradle: org.junit.jupiter:junit-jupiter-params:5.3.1' to classpath  

要解决第三个错误,请将光标放在第26行的Calculator上,然后按ALT+Enter
点击第一个选项

Add dependency on module 'main' 

enter image description here

enter image description here

然后在Gradle选项卡下运行verification-> test,它成功返回了预期结果

Testing started at 2:56 PM ...
2:56:07 PM: Executing task 'test'...

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
com.example.project.CalculatorTests > addsTwoNumbers() PASSED
com.example.project.CalculatorTests > add(int, int, int)[1] PASSED
com.example.project.CalculatorTests > add(int, int, int)[2] PASSED
com.example.project.CalculatorTests > add(int, int, int)[3] PASSED
com.example.project.CalculatorTests > add(int, int, int)[4] PASSED
BUILD SUCCESSFUL in 6s
3 actionable tasks: 3 executed
2:56:13 PM: Task execution finished 'test'.

enter image description here

enter image description here

然后,在CalculatorTest.java中运行单个测试时,通过单击左空白处的绿色箭头并选择收到的Run 'addsTowNumbers()'

Exception in thread "main" java.lang.NoClassDefFoundError:  
   org/junit/platform/launcher/TestExecutionListener

enter image description here

enter image description here

编辑

现在可以进行个人测试

昨天我保存了项目,然后加载了Maven sample,但同样失败了。今天起床后,我决定修改Gradle示例。当我单击左边距中的箭头进行单个测试时,它这次起作用了。目前我能想到的唯一原因是,在运行Maven示例时发生了某些更改,或者在关闭并重新打开项目后发生了更改。

CalculatorTest.java

/*
 * Copyright 2015-2018 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */

package com.example.project;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class CalculatorTests {

    @Test
    @DisplayName("1 + 1 = 2")
    void addsTwoNumbers() {
        Calculator calculator = new Calculator();
        assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
    }

    @ParameterizedTest(name = "{0} + {1} = {2}")
    @CsvSource({
            "0,    1,   1",
            "1,    2,   3",
            "49,  51, 100",
            "1,  100, 101"
    })
    void add(int first, int second, int expectedResult) {
        Calculator calculator = new Calculator();
        assertEquals(expectedResult, calculator.add(first, second),
                () -> first + " + " + second + " should equal " + expectedResult);
    }
}

对第20行进行测试的结果:class CalculatorTests {

enter image description here

enter image description here

对第24行进行测试的结果:void addsTwoNumbers() {

enter image description here

enter image description here

对第36行进行测试的结果:void add(int first, int second, int expectedResult) {

enter image description here

enter image description here

运行Gradle任务的结果:test

enter image description here

enter image description here

所选测试很重要

CalculatorTests创建了两个Run/Debug Configurations

  1. CalculatorTests具有Use classpath of module:test并引发异常。
  2. CalculatorTests (1)Use classpath of module:一起拥有junit5-jupiter-starter-gradle_test,并成功完成。

1 个答案:

答案 0 :(得分:0)

IntelliJ IDEA(针对此问题,社区2018.2)有时会为JUnit5测试生成无效的配置。

要从菜单中查看运行配置,请选择:运行->编辑配置...

enter image description here

两个不同的结果

在此示例中,由IntelliJ IDEA生成的Class CalculatorTests有两种运行配置。

1。

第一个配置会生成异常:

Exception in thread "main" java.lang.NoClassDefFoundError:   
  org/junit/platform/launcher/TestExecutionListener

enter image description here

enter image description here

2。

第二个配置正确运行

enter image description here

enter image description here

摘要

因此,通过单击左侧空白处的绿色箭头来运行单个测试时,测试会导致异常。检查实际正在运行的配置

enter image description here

,然后检查测试的配置详细信息

enter image description here

确保正确设置Use classpath or module