简体   繁体   中英

JUnit Tests error: java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException

First of all I'm using VSCODE (using the Java Test Runner extension) to write this program in Java and when trying to run tests I receive this error:

java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createFilteredTest(JUnit5TestLoader.java:70)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:64)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:526)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.PreconditionViolationException
    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)
    ... 7 more

I'm using Gradle and here are my dependencies:

dependencies {
    implementation 'org.hyperledger.fabric-chaincode-java:fabric-chaincode-shim:1.4.3'
    implementation 'com.owlike:genson:1.5'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.1'

    testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
    testImplementation 'org.assertj:assertj-core:3.11.1'
    testImplementation 'org.mockito:mockito-core:2.+'
}

this page maybe can provide you some informations:

https://github.com/iluwatar/java-design-patterns/issues/1007

as it talked about, maybe you need 'junit-platform-commons'.

How about:

  • List Upgrade the org.junit.jupiter:junit-jupiter to 5.6.0

Or

  • Adding testImplementation('org.junit.platform:junit-platform-launcher:1.5.2')

I got the same error with Visual Studio Code 1.53.2 in a Gradle 6.8.2 project when I switched to JUnit 5 from JUnit 4. To switch to JUnit 5, I made the following changes to my build script build.gradle as instructed in this Gradle user guide :

test {
    useJUnitPlatform()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

While the above allowed me to run .\gradlew.bat test successfully, Visual Studio Code wouldn't allow me to run the tests from within the editor itself and would abort with the error shared in the post.

To solve this, I followed the instructions here ie in VS Code, press F1 and type/select 'Java: Clean Java Language Server Workspace'. This solved the above error and JUnit 5 tests now run from within the editor again.

I experienced this issue as well, and found the solution was to upgrade the version of JUnit Jupiter API.

  • Windows: 10.0.19044
  • VSCode: 1.67.1
  • Test Runner for Java plugin: v0.35.0

The version of JUnit Jupiter API that didn't work and gave me the following error was 5.3.1 :

java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createFilteredTest(JUnit5TestLoader.java:70)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:64)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:513)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.PreconditionViolationException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
URLClassLoader.java:381
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
ClassLoader.java:424
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
Launcher.java:349
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
ClassLoader.java:357
    ... 7 more

The version of JUnit Jupiter API that works is 5.8.2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM