问题描述
在Windows 10上使用IntelliJ IDEA版本2018.2.5(社区版)并学习如何使用JUnit 5 参数化测试来自JUnit团队的JUnit 5示例.
Using IntelliJ IDEA version 2018.2.5 (Community Edition) on Windows 10 and learning how to use JUnit 5 parameterized tests with Gradle 4.8 based on JUnit 5 samples from the JUnit team.
该测试与gradle任务 test 一样按预期运行,但有时在运行单个测试时会获得
The test run as expected as gradle task test but sometimes when running individual test get
Exception in thread "main" java.lang.NoClassDefFoundError:
org/junit/platform/launcher/TestExecutionListener
在仍然使用IntelliJ 2018.2.5(Community Edition),带有参数测试的JUnit 5和Gradle的情况下如何解决此异常?
How can this exception be fixed while still using IntelliJ 2018.2.5 (Community Edition), JUnit 5 with parameter test and using Gradle?
我一直在试图找到答案,但是我遇到的大多数答案都表明,将IntellJ与JUnit 5结合使用正处于许多过渡之中,因此不确定该信任什么.
I have been trying to find an answer for this but most of what I run into are answers that note the use of IntellJ with JUnit 5 are in a lot of transition so not sure what to trust.
我知道有比正常或超出正常水平更多的细节和屏幕截图,但是当您学习新知识并需要帮助时,开始寻找信息时,您会厌倦看到那些遗漏了一些重要细节的答案.因此,对于那些在同一条船上并且需要看到缺少的细节的人,希望我已将其包括在这里.虽然可能不是您要寻求的问题,但此处的详细信息可能会对您有所帮助.
I know there is way more detail and screen shots than normal or beyond normal, but when you are learning something new and need help and you start looking for info, you get tired of seeing answers that leave out some important detail. So for those that are in the same boat and need to see that missing detail hopefully I have included it here. While this question may not be the one you seek, the details here may help you.
异常堆栈跟踪
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
Microsoft Windows Version 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位服务器VMProject SDK:11(Java版本"11.0.1")对于此示例,IntelliJ是干净版本.
IntelliJ IDEA 2018.2.5 (Community Edition)
Build #IC-182.4892.20, build on October 16, 2018JRE: 1.8.0_152-release-1248-b19 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Project SDK: 11 (java version "11.0.1")IntelliJ is clean version installed for this example.
使用的特定JUnit示例: junit5-jupiter -starter-gradle
Specific JUnit sample used: 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
已下载 junit5-samples
未压缩的文件
在目录junit5-samples-r5.3.1
下将junit5-jupiter-starter-gradle
复制到C:\Users\Eric\IdeaProjects
Downloaded junit5-samples
Uncompressed files
Under directory junit5-samples-r5.3.1
copied junit5-jupiter-starter-gradle
to C:\Users\Eric\IdeaProjects
在欢迎屏幕上使用IntelliJ 导入项目来加载项目.
Used IntelliJ Import Project on welcome screen to load project.
为导入项目"对话框选项选择默认选项,例如单击所有Import Project
对话框页面的下一步"或完成".
Took default options for Import Project dialog options, e.g. click 'Next' or 'Finish' for all Import Project
dialog pages.
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
When project opened received Event log about Unlinked Gradle project
and clicked Import Gradle project
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任务构建项目:构建
Built project using Gradle task: build
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
Closed panel on right
Using Project
panel on left
Expand directories to reveal test/java/com.example.project/CalculatorTests.java
Double clicked CalculatorTests.java
要解决第一个错误,请将光标放在第13行的Assertions
上,然后按ALT+Enter
点击第一个选项
To fix first error put cursor on Assertions
on line 13 and press ALT+Enter
Click first option
Add library 'Gradle: org.junit.jupiter:junit-jupiter-api:5.3.1' to classpath
要解决第二个错误,请将光标放在第17行的ParameterizedTest
上,然后按ALT+Enter
点击第一个选项
To fix second error put cursor on ParameterizedTest
on line 17 and press ALT+Enter
Click first option
Add library 'Gradle: org.junit.jupiter:junit-jupiter-params:5.3.1' to classpath
要解决第三个错误,请将光标放在第26行的Calculator
上,然后按ALT+Enter
点击第一个选项
To fix third error put cursor on Calculator
on line 26 and press ALT+Enter
Click first option
Add dependency on module 'main'
然后在Gradle选项卡下运行verification
-> test
,该操作成功返回了预期结果
Then under Gradle tab ran verification
-> test
which successfully returned expected results
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'.
然后在CalculatorTest.java中运行单个测试时,通过单击左空白处的绿色箭头并选择收到的Run 'addsTowNumbers()'
:
Then when running individual test in CalculatorTest.java by clicking on green arrow in left margin and selecting Run 'addsTowNumbers()'
received:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/junit/platform/launcher/TestExecutionListener
昨天我保存了项目,然后加载了 Maven示例也失败了.今天起床后,我决定再试一试Gradle示例.当我单击左边距中的箭头进行单个测试时,它这次起作用了.目前我能想到的唯一原因是,在运行Maven示例时发生了一些更改,或者在关闭并重新打开项目后发生了更改.
Yesterday I saved the project and then loaded up the Maven sample which also failed. When I woke up today I and decided to tinker with the Gradle sample some more. When I clicked on the arrow in the left margin for a single test it worked this time. The only reason at present I can think of for why it works now is that in running the Maven sample something was changed, or in closing and reopening the project something changed.
CalculatorTest.java
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 {
Result of running test for line 20: class CalculatorTests {
对第24行进行测试的结果:void addsTwoNumbers() {
Result of running test for line 24: void addsTwoNumbers() {
第36行的运行测试结果:void add(int first, int second, int expectedResult) {
Result of running test for line 36: void add(int first, int second, int expectedResult) {
运行Gradle任务的结果:test
Result of running Gradle task: test
创建了两个运行/调试配置对于CalculatorTests
-
CalculatorTests
的Use classpath of module:
和test
并引发异常. -
CalculatorTests (1)
具有Use classpath of module:
和junit5-jupiter-starter-gradle_test
并成功完成.
CalculatorTests
hasUse classpath of module:
withtest
and throws the exception.CalculatorTests (1)
hasUse classpath of module:
withjunit5-jupiter-starter-gradle_test
and completes successfully.
推荐答案
IntelliJ IDEA(针对此问题,社区2018.2)有时会为JUnit5测试生成无效的配置.
IntelliJ IDEA (for this question Community 2018.2) will sometimes generate an invalid configuration for a JUnit5 test.
要从菜单中查看运行配置,请选择:运行->编辑配置...
To see the run configuration from the menu choose: Run -> Edit Configurations...
在此示例中,由IntelliJ IDEA生成的Class CalculatorTests有两种运行配置.
In this example there are two run configurations for Class CalculatorTests generated by IntelliJ IDEA.
第一个配置会生成异常:
The first configuration generates the exception:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/junit/platform/launcher/TestExecutionListener
第二个配置正确运行
因此,通过单击左侧空白处的绿色箭头来运行单个测试时,测试会导致异常.检查实际正在运行的配置
So when running individual test by clicking on the green arrow in the left margin and the test results in an exception. Check which configuration is actually running
,然后检查测试的配置详细信息
and then check the configuration details for the test
以确保正确设置Use classpath or module
.
这篇关于IntelliJ JUnit 5测试可以作为Gradle任务正常运行.当作为单独的测试运行时,有时会出现异常:NoClassDefFoundError .../TestExecutionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!