【问题标题】:java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException when trying run junit5 test with mavenjava.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException 尝试使用 maven 运行 junit5 测试
【发布时间】:2019-11-24 05:04:10
【问题描述】:

当尝试使用命令 mvn test 运行测试时,我收到一个错误:

[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)

我在 IntelliJ 2008.1 下运行它,使用 maven 3.6.1 和版本 2.22.1 中的 surefire 插件

我在 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>

最近我无法运行任何测试用例,但现在经过一些配置尝试后我收到此错误。

【问题讨论】:

  • mvn clean 运行测试?我对此表示怀疑....删除平台启动器 运行 JUnit 5 测试通常需要 junit-jupiter-engine ...如果您有参数化测试,则需要 junit-jupiter-params ...此外,我建议使用 Junit Jupiter Team 的 bom 更容易处理......

标签: maven junit5


【解决方案1】:

删除junit-platform-launcherjunit-jupiter-enginejunit-jupiter-api

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

来源:

【讨论】:

  • 就我而言,我实际上同时拥有junit-jupiterjunit-jupiter-params。这导致了碰撞。拥有junit-jupiter 就是您所需要的。
  • java.lang.ClassNotFoundException: org.junit.platform.engine.EngineDiscoveryListener 失败
【解决方案2】:

在我的情况下添加以下依赖项对我有帮助

 <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.5.2</version>
 </dependency>

【讨论】:

【解决方案3】:

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')
}

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

【讨论】:

  • 谢谢!我在 VScode 上尝试了这个,得到了同样的 No class def found 错误。对我来说,只需添加依赖项即可!
【解决方案4】:

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

junit-jupiter artifact 引入了所有其他必要的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>

【讨论】:

    【解决方案5】:

    正如其他人所建议的,使用 JUnit 5.5.2 版本是这里的方法。 有不同的选择来实现这一点:

    1. 如果您使用的是spring-boot-starter-parent,您可以将其升级为2.2.0.RELEASE
    2. 如果你使用spring-boot-starter-parent(或spring-boot-dependencies),你可以定义一个属性来更新JUnit:&lt;junit-jupiter.version&gt;5.5.2&lt;/junit-jupiter.version&gt;
    3. 如果您只是在 Eclipse 中遇到此问题,您可以更新它并将 JUnit 5 库添加到 Java 构建路径(项目 > Java 构建路径 > 库 > 添加库 > JUnit > JUnit 5 > 完成)
    4. 您可以使用 5.5.2 版本添加 Junit BOM(请参阅 Prasanth RajendranRamit 答案)

    【讨论】:

    • 完美答案(3 号固定我)
    【解决方案6】:

    我在 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>
    

    并且我在 java build path(project(右键单击->java build path->libraries->junit->junit5)中添加了junit5 它对我有用。我的是spring-boot项目。

    【讨论】:

      【解决方案7】:

      在我的情况下,删除我的本地 maven 存储库后问题就消失了。 不知道是什么库导致了这种奇怪的行为,但现在一切正常!

      【讨论】:

        【解决方案8】:

        java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException 尝试使用 maven 运行 junit5 测试时

        这个错误可能是由于junit-plateform-engine的版本不匹配造成的 junit-jupiter-params 和 junit-platform-runner 以及其他各自的依赖项。

        所以为了解决这个问题,您不需要定义像 1.5.3 ...等这样的版本

        你可以定义版本如下:

        <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>
        

        所以通过这个兼容版本的jar会被添加到你的项目中,项目就会成功运行

        【讨论】:

          【解决方案9】:

          如果它对任何人有帮助: 在 Eclipse(基于 gradle)中运行 JUnit 5 测试时,我遇到了类似的错误以及“没有使用测试运行程序 JUnit 5 找到测试”。根据https://github.com/eclipse/buildship/issues/980

          ,升级到 JUnit 5.6.0 有所帮助

          【讨论】:

            【解决方案10】:

            如果在生产代码中包含一些测试类(来自 junit/assertj),您可能会收到类似“java.lang.NoClassDefFoundError: org/junit/platform/commons/util/Preconditions”的错误。

            以上是

            的结果
            • 将测试库作为依赖项放在不在测试范围内(因此使其在运行时可用)
            • 可能由于上述错误,您在生产代码中导入此类测试实用程序类 - src/main/java(例如,您在来自 src/main/java 的生产代码中使用了类似 import org.assertj.core.util.Lists 的导入。
            • 也有可能是此类测试库被添加为 java9 模块信息的一部分(如 requires org.assertj.core

            注意:这种有问题的依赖项可能在您的模块内部或依赖项中包含的任何模块中。

            例如你的模块有以下依赖:

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

            并且您的另一个模块在范围编译中包含 spring-boot-starter-test,例如:

                <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>
            

            正如上面代码 sn-p 中提到的,范围甚至可能被省略,默认为“编译”。

            现在出现该错误后,您的模块中可能有一个生产类 YourClass,并且不小心从测试库中导入了一些测试代码,例如:

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

            解决办法是:

            • 修复依赖项,使其在范围测试中并在生产代码中使用正确的导入
            • 从生产代码中不需要的任何测试库中清理 module-info.java

            【讨论】:

              【解决方案11】:

              几天前我遇到了类似的问题。我尝试了这里建议的所有解决方案,但都没有奏效。在我的情况下,发生错误是因为类路径最终有两个不同版本的 JUnit。我有 spring-boot-test 依赖,它使用 JUnit 5.3.2。但后来,我添加了JUnit 5.7.1。因此,我的项目是使用较新版本的 JUnit (5.7.1) 编译的,但在运行时发现了较旧版本的 (5.3.2)。结果,JUnit 启动器尝试使用旧版本 JUnit 中不可用的类。在我的情况下,解决方案是覆盖 Spring 管理的 JUnit 版本,如下所示:

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

              希望有人觉得这有帮助。

              【讨论】:

                猜你喜欢
                • 2020-05-13
                • 2021-10-16
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-08-31
                • 1970-01-01
                • 2013-08-23
                相关资源
                最近更新 更多