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

kri*_*2pl 6 maven junit5

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)
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

...

<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
    </plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)

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

Ger*_*oza 21

正如其他人建议的那样,使用 JUnit5.5.2版本是这里的方法。有不同的替代方案可以实现这一目标:

  1. 如果您正在使用spring-boot-starter-parent,则可以将其升级为2.2.0.RELEASE
  2. 如果您使用spring-boot-starter-parent(或spring-boot-dependencies),您可以定义一个属性来仅更新 JUnit:<junit-jupiter.version>5.5.2</junit-jupiter.version>
  3. 如果您仅在 Eclipse 中遇到此问题,您可以更新它并将 JUnit 5 库添加到 Java 构建路径(Project > Java Build Path > Libraries > Add Library > JUnit > JUnit 5 > Finish
  4. 您可以使用 5.5.2 版本添加 Junit BOM(请参阅Prasanth RajendranRamit答案)


Pra*_*ran 11

我遇到了同样的问题,Gradle build我使用 解决了我的问题JUnit Bill of Materials(BOM),它将处理 Junit 的直接和可传递依赖项版本。

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')
}
Run Code Online (Sandbox Code Playgroud)

注意:我没有指定版本,因为 Junit BOM 将负责 Junit 依赖项的版本管理角色。


小智 9

以下使用surefire.plugin.version 2.22.2对我有用

junit-jupiter artifact 引入了所有其他必要的工件。

<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>
Run Code Online (Sandbox Code Playgroud)


Ind*_*942 7

删除junit-platform-launcherjunit-jupiter-engine然后junit-jupiter-api

添加junit-jupiter。(junit-jupiter是聚合器)

资料来源:

  • 就我而言,实际上我同时拥有“junit-jupiter”和“junit-jupiter-params”。这导致了碰撞。拥有“junit-jupiter”就是您所需要的。 (2认同)
  • 失败并显示“java.lang.ClassNotFoundException:org.junit.platform.engine.EngineDiscoveryListener” (2认同)

小智 5

添加以下依赖关系对我的情况有所帮助

 <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.5.2</version>
 </dependency>
Run Code Online (Sandbox Code Playgroud)