2017-09-23 201 views
3

我創建了一個簡單的測試,嘗試Junit的5:java.lang.NoSuchMethodError當使用JUnit測試運行5

import org.junit.jupiter.api.Test; 
public class MyTest { 

    @Test 
    public void testJupiter() { 
     System.out.println("test"); 
    } 
} 

這是依賴我用:

 <dependency> 
      <groupId>org.junit.jupiter</groupId> 
      <artifactId>junit-jupiter-api</artifactId> 
      <version>5.0.0</version> 
      <scope>test</scope> 
     </dependency> 

堆棧跟蹤是下一個:

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) 

任何想法出了什麼問題?

+0

堆棧跟蹤信息的更多的線將是有益的 – MondKin

+0

請分享@Test標註的進口以及 – shippi

回答

1

除了依賴關係外,您是否還在您的pom.xml中配置了JUnit5插件?它應該像這樣(從the docs拍攝):

... 
<build> 
    <plugins> 
     ... 
     <plugin> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.19</version> 
      <dependencies> 
       <dependency> 
        <groupId>org.junit.platform</groupId> 
        <artifactId>junit-platform-surefire-provider</artifactId> 
        <version>1.0.0</version> 
       </dependency> 
       <dependency> 
        <groupId>org.junit.jupiter</groupId> 
        <artifactId>junit-jupiter-engine</artifactId> 
        <version>5.0.0</version> 
       </dependency> 
      </dependencies> 
     </plugin> 
    </plugins> 
</build> 
... 
<dependencies> 
    ... 
    <dependency> 
     <groupId>org.junit.jupiter</groupId> 
     <artifactId>junit-jupiter-api</artifactId> 
     <version>5.0.0</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 
... 
+0

我嘗試添加該插件。它只有在我運行mvn clean install時纔有所幫助。但是,如果我右鍵點擊測試,然後選擇「運行」,它仍然失敗,同樣的錯誤。事實證明原因在於Intellij想法的特定依賴版本。但非常感謝! – IKo