zoukankan      html  css  js  c++  java
  • Idea 运行测试NoSuchMethodError Junit5

    1.背景

    环境:

    macOS 10.13.4 

    idea: 2016.3

    springboot 版本: 2.2.2.RELEASE

    pom依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>

    2.情况描述

    之前用的是springboot1.5.9, 想用springboot2.x 写个demo看看,直接使用Idea新建了个springboot工程,然后一路next。

    项目建完,写了个小case,想跑下test。 结果test类跑不起来,错误大概(没保存现场,这个是从网上找的类似的)如下:

    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)

    通过查询,得知原因是:IDEA版本过低不支持JUnit5引起的。我的是2016.3

    然后junit包依赖如下:

     网上的解决方案有

    1). 导入Junit4的依赖,使用Junit4的测试包跑----> @SpringbootTest 还是会报错

    2). Junit 依赖版本增加使用-M2

    3). 剔除Junit5依赖。我的情况非此类,看我的依赖知道,我的是项目直接使用的Junit5。

    4). 我甚至找到了一个跟我一模一样的,我自己的仍旧有问题。参考来源: http://leakfromjavaheap.blogspot.com/2016/12/playing-with-junit-5-and-intellij-idea.html

    5). 升级IDEA 

    3.解决方案

    采用升级IDEA完美解决,似乎是2017.3以后的版本就行,我直接升级到了2019.2。考虑到一劳永逸的问题,所以直接升级的。没有直接升级到2019.3的原因,是担心最新版不稳定。以上

    4.后记

    1. 关于升级:

      升级2019.2或者2019.3后,升级后明显感觉卡顿了...(可能是我电脑硬件也该升级了吧)。通过尝试,发现2019.2编辑pom文件的时候,卡顿到怀疑人生,查询到可能是maven本地index的问题;2019.3,建议内存16G的升级,我的电脑老版本8G的,也不流畅;推荐升级2019.1, 毕竟稳定和流畅最重要。

    2. 关于junit

      网上很多人说springboot2.x默认使用junit4,实际尝试发现,其实是springboot2.1.x之前的版本,我的2.2.2.RELEASE,默认会用到junit5.

    3. springboot和springcloud版本

      一定要注意cloud和boot的版本,防止不兼容的坑。软件建议别上最新版,无论是学习还是work

  • 相关阅读:
    Java连接各种数据库的实例
    解决Android SDK Manager 更新、下载慢以及待安装包列表不显示
    This version of the rendering library is more recent than your version of ADT plug-in. Please update
    注册asp.net 4.0 到iis
    linux下常用的截图、录屏工具
    汉化Eclipse+配色方法(官方语言包)
    用了皮肤控件之后,报错:容量超出了最大容量 参数名:capacity
    自定义DateTimeInput(时间)控件的显示格式
    SQL语句增加列、修改列类型、修改列、删除列
    GDI+ DrawString字间距问题
  • 原文地址:https://www.cnblogs.com/LordNeo/p/12029515.html
Copyright © 2011-2022 走看看