44

When trying to run tests using command mvn test I receive an error:

[ERROR] There was an error in the forked process
[ERROR] java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
[ERROR] java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
[ERROR]         at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:656)
[ERROR]         at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:282)
[ERROR]         at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:245)
[ERROR]         at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1183)
[ERROR]         at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1011)
[ERROR]         at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:857)
[ERROR]         at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
[ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
[ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
[ERROR]         at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
[ERROR]         at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR]         at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR]         at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
[ERROR]         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
[ERROR]         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR]         at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR]         at org.apache.maven.cli.MavenCli.execute(MavenCli.java:956)
[ERROR]         at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
[ERROR]         at org.apache.maven.cli.MavenCli.main(MavenCli.java:192)
[ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
[ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
[ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
[ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)

I run it under IntelliJ 2008.1, using maven 3.6.1 and surefire plugin in version 2.22.1

I have following dependencies in pom:

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.5.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.5.0</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.5.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.5.0</version>
    <scope>test</scope>
</dependency>

...

<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
    </plugin>
</plugins>

Recently I wasn't able to run any test cases but now after some configuration tries I receive this error.

kris82pl
  • 977
  • 3
  • 12
  • 21
  • 1
    `mvn clean ` runs tests? I have my doubts about that....Remove platform-launcher only junit-jupiter-engine is usually needed for running JUnit 5 tests ...If you have parameterized tests you need junit-jupiter-params ...furthermore I recommend to use the bom's of the Junit Jupiter Team is much easier to handle... – khmarbaise Jul 15 '19 at 13:36

13 Answers13

51

Remove junit-platform-launcher, junit-jupiter-engine and junit-jupiter-api.

Add junit-jupiter. (junit-jupiter is aggregator)

Sources:

IndustryUser1942
  • 1,052
  • 1
  • 8
  • 12
  • 2
    In my case I actually had them both `junit-jupiter` and `junit-jupiter-params`. This resulted in collision. Having `junit-jupiter` is all you need. – labm0nkey Dec 30 '19 at 15:24
  • 3
    Fails with `java.lang.ClassNotFoundException: org.junit.platform.engine.EngineDiscoveryListener` – Kango_V Oct 22 '20 at 13:30
29

adding following dependency helped in my case

 <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.5.2</version>
 </dependency>
  • [junit-platform-commons-1.5.2.pom](https://repo1.maven.org/maven2/org/junit/platform/junit-platform-commons/1.5.2/junit-platform-commons-1.5.2.pom) contains only `apiguardian-api` artifact and I am still wondering how could this solve the dependency conflict – Prasanth Rajendran Oct 21 '19 at 19:28
  • This worked for me as well but can you please explain why it resolves this error? – Fakipo Sep 04 '22 at 12:21
22

As others suggested, using JUnit 5.5.2 version is the way to go here. There are different alternatives to achieve this:

  1. If you're using spring-boot-starter-parent, you can upgrade it to 2.2.0.RELEASE
  2. If you use spring-boot-starter-parent (or spring-boot-dependencies), you can define a property to update just JUnit: <junit-jupiter.version>5.5.2</junit-jupiter.version>
  3. If you're having this issue just in Eclipse, you can update it and add the JUnit 5 library to the Java Build Path (Project > Java Build Path > Libraries > Add Library > JUnit > JUnit 5 > Finish)
  4. You can add the Junit BOM, using 5.5.2 version (see Prasanth Rajendran or Ramit answer)
Gerardo Roza
  • 2,746
  • 2
  • 24
  • 33
11

I got the same problem with Gradle build and I got my issue resolved using the JUnit Bill of Materials(BOM), which will take care of Junit's direct and transitive dependencies version.

dependencyManagement {
    imports {
        mavenBom "org.junit:junit-bom:5.5.2"
    }
}
dependencies {
...
    testCompile('org.junit.jupiter:junit-jupiter-api')
    testRuntime('org.junit.jupiter:junit-jupiter-engine')
    testCompile('org.junit.jupiter:junit-jupiter-params')
    testCompile('org.junit.platform:junit-platform-launcher')
    testCompile('org.junit.platform:junit-platform-runner')
}

NOTE: I have not specified the version because the Junit BOM will take care of the Junit dependencies' version management role.

Prasanth Rajendran
  • 4,570
  • 2
  • 42
  • 59
  • Thank you! I was trying this on VScode, was getting the same No class def found error. For me, simply adding the dependencies worked! – Shivansh Jagga Apr 08 '20 at 09:03
10

The following worked for me using surefire.plugin.version 2.22.2

junit-jupiter artifact brings in all other necessary artifacts.

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.junit</groupId>
      <artifactId>junit-bom</artifactId>
      <version>5.5.2</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
  </dependency>
  <!--Optional: Supports running Junit4 along with Junit5 -->
  <dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
Ramit
  • 181
  • 3
  • 2
3

Few days ago I faced the similar issue. I tried all solutions suggested here, but none of them worked. In my case the error occurred because classpath ended up having two different versions of JUnit. I had spring-boot-test dependency wich uses JUnit 5.3.2. But then, I added JUnit 5.7.1. Therefore, my project was compiled with a newer version of JUnit (5.7.1) but found an older version (5.3.2) at runtime. As a result, the JUnit launcher tried to use a class that is not available in the older version of JUnit.In my case solution was to override the JUnit version managed by Spring like this:

<properties>
    <junit-jupiter.version>5.7.1</junit-jupiter.version>
</properties>

Hope somebody find this helpful.

Nemanja
  • 3,295
  • 11
  • 15
1

In my case the problem disappeared after deleting my local maven repository. Don't know what library caused this strange behaviour, but now everything is working fine!

tangens
  • 39,095
  • 19
  • 120
  • 139
1

You can get the error like "java.lang.NoClassDefFoundError: org/junit/platform/commons/util/Preconditions" in case of tring to included some test class (from junit/assertj) in your production code.

Above is a consequence of

  • puttinh test library as a dependency NOT in scope test (so making it available at runtime)
  • possibly following on above error you importing such test utility class in your production code - src/main/java (for example you used import like import org.assertj.core.util.Lists in your production code from src/main/java.
  • the possibility is also that such test library was added as part of java9 module-info (like requires org.assertj.core)

NOTE: This offending dependency could be inside your module or any module included in dependencies.

For example your module has following dependency:

    <dependency>
        <groupId>com.acme</groupId>
        <artifactId>your-another-module</artifactId>
    </dependency>

and your another module includes spring-boot-starter-test in scope compile like:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>compile</scope> <!-- **or even scope is omitted and by default it is compile !** -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

As mentioned inside above code snippet, the scope might have even been omitted which would default to "compile".

Now following on that error you might have a production class YourClass residing in your module and accidentally import some test code from test libraries like:

// here we have accidential import of Lists from assertj
import org.assertj.core.util.Lists;
class YourClass {

}

The solution is to:

  • fix the dependencies to make them in scope test and use correct imports in your production code
  • clean up module-info.java from any test libraries which are not needed in your production code
walkeros
  • 4,736
  • 4
  • 35
  • 47
1

Try upgrade the Junit to 5.8.2 or greater, see Jupter Junit versions availables here:

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.8.2</version>
    <scope>test</scope>
</dependency>

The junit-platform-commons is transactive dependency from Junit, so if you upgrade Junit these transactive dependencies will also be updated.

0

I have commented the below part in pom.xml:

  <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.3.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.3.2</version>
      <scope>test</scope>
    </dependency>

And I have added junit5 in java build path(project(right click->java build path->libraries->junit->junit5) It worked for me. Mine is spring-boot project.

Boney
  • 1,462
  • 1
  • 11
  • 19
0

java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException when trying run junit5 test with maven

This error can be come due to the version mismatch of junit-plateform-engine junit-jupiter-params and junit-platform-runner and also other respective dependencies.

So for the resolution of this issue you do not need to define version like 1.5.3 ...etc

you can define version as follows :

<dependencies>
<!--need to add for parameterized test -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        **<version>${junit.jupiter.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.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        **<version>${junit.platform.version}</version>**
        <scope>test</scope>
    </dependency>
</dependencies>

So through this compatible version jar will be added in your project and project will run successfully

0

In case it helps anyone: I was getting similar error along with "No tests found with test runner JUnit 5" when running JUnit 5 tests in Eclipse (gradle-based). Upgrading to JUnit 5.6.0 helped as per https://github.com/eclipse/buildship/issues/980

Jay-C
  • 23
  • 1
  • 6
0

I use gradle, Finally making below changes worked for me

Changed to ->

dependencies {
testImplementation 'org.mockito:mockito-junit-jupiter:3.11.1'
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'

}

from->

dependencies {
    testCompile 'org.mockito:mockito-junit-jupiter:3.3.3'
     testCompile 'org.junit.jupiter:junit-jupiter:5.4.2'
}
  • Hi and welcome to Stack Overflow! Your answer is proposing to use Gradle, but the question was about an error when using Maven. These are two different tools for the same task (dependency management). If you came here because you got the same error message, search for question with the tag [tag:gradle], or you can post a new question that is specific to Gradle and answer it yourself. To prevent downvotes of your answer and loosing reputation, you should probably delete this answer. – bbjay Mar 12 '23 at 22:46