SpringBoot单元测试报错:java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils

    当我在SpringBoot项目中想做单元测试时,运行就报以下错误

SpringBoot单元测试报错:java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils

    我的配置信息都是正确的,瞬间懵逼了,网上都说是Junit5与IDEA版本不兼容,需要升级IDEA的版本,或者使用Junit4来进行单元测试。IDEA升级怕又要**,很是蛋疼,所以还是想想使用Junit4来做测试了。

    网上也找了半天也没有太全的说明,自己研究了下,竟然搞成了,前戏完成,上菜了    

   新建SpringBoot(版本2.2.6)项目默认的pom文件:

SpringBoot单元测试报错:java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils

    pom文件中spring-boot-starter-test默认继承Junit5(JUnit Platform + JUnit Jupiter + JUnit Vintage),我们需要排除Junit5相关的依赖包,虽然上面默认pom文件默认排除了junit-vintage-engine,除此之外我们还要排除junit-jupiter-api,然后再加入Junit4的依赖,如下:

SpringBoot单元测试报错:java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils

这样就大功告成了!

我把默认排除的依赖去掉也可以测试成功。我没去升级IDEA版本,各位可以测试一下,成功后欢迎留言,谢谢!