zoukankan      html  css  js  c++  java
  • unit测试出现异常:Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util

    在进行单元测试时,测试出现异常

    Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
        at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:30)
        at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:53)
        at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:49)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

    https://junit.org/junit5/docs/current/user-guide/#running-tests-ide-intellij-idea

    pom.xml依赖如下

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

    问题原因 
    错误就在pom.xml的依赖中,仔细查看控制台输出你会发现IntelliJ IDEA正在尝试使用JUnit5运行我的测试用例。

    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)

    通过pom.xml发现,我希望使用JUnit4.12运行测试用例,我们查看pom.xml发现junit-jupiter-api这个依赖会导致这个错误。

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>RELEASE</version>
    </dependency>

    因为此模块专为JUnit5而设计 -https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api

    解决方案

    1、删除pom.xmlorg.junit.jupiter依赖 
    2、Reimport All Maven Project

  • 相关阅读:
    对字符串进行编码解码
    C#重新启动时,关闭较早的进程
    JS给HTML5页面<Select></Select>绑定选中项
    获取该项目路径下的文件路径
    IE浏览器从页面向后台Controller传中文值出现乱码问题
    委托
    win10激活命令
    判断窗口是否已经被打开
    JS之对象数组遍历?
    JS之arguments属性解读函数传参?
  • 原文地址:https://www.cnblogs.com/bodhitree/p/9456031.html
Copyright © 2011-2022 走看看