Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.NoClassDefFoundError:org/junit/platform/commons/PremissionViolationException尝试使用maven运行junit5测试时_Maven_Junit5 - Fatal编程技术网

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

java.lang.NoClassDefFoundError:org/junit/platform/commons/PremissionViolationException尝试使用maven运行junit5测试时,maven,junit5,Maven,Junit5,尝试使用命令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: Ther

尝试使用命令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)
我在Intellij2008.1下运行它,在2.22.1版本中使用maven 3.6.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>

org.junit.jupiter
JUnitJupiter api
5.5.0
测试
org.junit.jupiter
朱尼特木星发动机
5.5.0
测试
org.junit.jupiter
朱尼特朱庇特酒店
5.5.0
测试
org.junit.platform
junit平台发射器
1.5.0
测试


maven编译器插件
org.apache.maven.plugins
maven surefire插件

最近我无法运行任何测试用例,但在一些配置尝试之后,我收到了这个错误。

删除
junit平台启动器
junit jupiter引擎
junit jupiter api

添加
junitjupiter
。(
junitjupiter
is聚合器)

资料来源:

  • 这对我有用

在我的案例中,添加以下依赖项很有帮助

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

org.junit.platform
junit平台共享
1.5.2

我在Gradle build中遇到了同样的问题,我使用JUnit物料清单(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依赖项的版本管理角色

使用surefire.plugin.version 2.22.2,以下内容对我有效

junit jupiter工件带来所有其他必需的工件


org.junit
junit bom
5.5.2
聚甲醛
进口
org.junit.jupiter
朱尼特朱庇特
测试
org.junit.vintage
朱尼特老式发动机
测试

正如其他人所建议的那样,使用JUnit
5.5.2
版本是一种方法。 有不同的替代方案来实现这一点:

  • 如果您使用的是
    spring boot starter父版本
    ,则可以将其升级到
    2.2.0.版本
  • 如果使用
    springbootstarterparent
    (或
    springbootdependencies
    ),则可以定义一个只更新JUnit的属性:
    5.5.2
  • 如果您在Eclipse中遇到这个问题,您可以更新它并将JUnit5库添加到Java构建路径(项目>Java构建路径>库>添加库>JUnit>JUnit 5>完成)
  • 您可以使用5.5.2版本添加Junit BOM(请参阅Prasanth Rajendran或Ramit答案)

  • 我在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>
    
    
    org.junit.jupiter
    JUnitJupiter api
    5.3.2
    测试
    org.junit.jupiter
    朱尼特木星发动机
    5.3.2
    测试
    
    我在java构建路径中添加了junit5(项目(右键单击->java构建路径->库->junit->junit5)
    我的是spring boot项目。

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

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

    此错误可能是由于junit平台引擎的版本不匹配造成的 junit jupiter参数和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>
    
    
    org.junit.jupiter
    朱尼特朱庇特酒店
    **${junit.jupiter.version}**
    测试
    org.junit.jupiter
    朱尼特木星发动机
    **${junit.jupiter.version}**
    测试
    org.junit.platform
    junit平台跑步者
    **${junit.platform.version}**
    测试
    
    因此,通过此兼容版本,jar将添加到您的项目中,项目将成功运行,以防对任何人有帮助:
    在Eclipse(基于gradle)中运行JUnit 5测试时,我遇到了类似的错误以及“没有找到使用测试运行程序JUnit 5的测试”。升级到JUnit 5.6.0有助于按照

    进行操作,如果尝试包含一些测试类,您可以得到类似“java.lang.NoClassDefFoundError:org/JUnit/platform/commons/util/Premissions”的错误(来自junit/assertj)在您的生产代码中

    以上是

    • puttinh测试库作为不在范围测试中的依赖项(因此使其在运行时可用)
    • 可能在上面的错误之后,您在生产代码中导入了这样的测试实用程序类-
      src/main/java
      (例如,您在生产代码中使用了import-like
      import-org.assertj.core.util.Lists
      src/main/java
    • 还有一种可能性是,这种测试库是作为java9模块信息的一部分添加的(比如
      需要org.assertj.core
    注意:这个有问题的依赖项可能在您的模块或依赖项中包含的任何模块内

    例如,您的模块具有以下依赖项:

        <dependency>
            <groupId>com.acme</groupId>
            <artifactId>your-another-module</artifactId>
        </dependency>
    
    解决办法是:

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

    mvn clean
    运行测试?我对此表示怀疑…运行junit 5测试通常只需要删除平台启动器junit jupiter引擎…如果您有参数化测试,则需要junit jupiter参数…此外,我建议使用junit jupiter团队的bom更易于处理…仅包含
     阿皮古
    
        <dependency>
            <groupId>com.acme</groupId>
            <artifactId>your-another-module</artifactId>
        </dependency>
    
        <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>
    
    // here we have accidential import of Lists from assertj
    import org.assertj.core.util.Lists;
    class YourClass {
    
    }