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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM