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.
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
How can this exception be fixed while still using IntelliJ 2018.2.5 (Community Edition), JUnit 5 with parameter test and using Gradle?
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.
The exception stack trace
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
Software Versions
Microsoft Windows Version 10.0.171314.345
IntelliJ IDEA 2018.2.5 (Community Edition)
Build #IC-182.4892.20, build on October 16, 2018
JRE: 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.
Specific JUnit sample used: junit5-jupiter-starter-gradle
Gradle: 4.8
build.gradle - exactly same as in demo
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'
}
Gradle runner: Platform Test Runner
Downloaded junit5-samples
Uncompressed files
Under directory junit5-samples-r5.3.1
copied junit5-jupiter-starter-gradle
to C:\Users\Eric\IdeaProjects
Used IntelliJ Import Project on welcome screen to load 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
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
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
Files cached by 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
Closed panel on right
Using Project
panel on left
Expand directories to reveal test/java/com.example.project/CalculatorTests.java
Double clicked CalculatorTests.java
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
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
To fix third error put cursor on Calculator
on line 26 and press ALT+Enter
Click first option
Add dependency on module 'main'
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'.
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
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
/*
* 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);
}
}
Result of running test for line 20: class CalculatorTests {
Result of running test for line 24: void addsTwoNumbers() {
Result of running test for line 36: void add(int first, int second, int expectedResult) {
Result of running Gradle task: test
There are two Run/Debug Configurations created for CalculatorTests
CalculatorTests
has Use classpath of module:
with test
and throws the exception.CalculatorTests (1)
has Use classpath of module:
with junit5-jupiter-starter-gradle_test
and completes successfully.Run Gradle testsIn your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.
Test detection By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.
single system property can be used to specify a single test. You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.
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...
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
The second configuration runs correctly
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
to make sure Use classpath or module
is set correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With