2016-12-07 176 views
3

試圖用IntelliJ IDEA學習JUnit 5。創建一個測試類並嘗試測試一種方法。這裏的消息(見第三行):JUnit說我的測試方法「無法解析爲獨特的方法」

INFO: Discovered TestEngines with IDs: [junit-jupiter] 
Internal Error occurred. 
org.junit.platform.commons.util.PreconditionViolationException:'DirectoryTest#directory2bytes' could not be resolved to a unique method 
at org.junit.platform.engine.discovery.DiscoverySelectors.lambda$selectMethod$1(DiscoverySelectors.java:131) 
at java.util.Optional.orElseThrow(Optional.java:290) 
at org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod(DiscoverySelectors.java:130) 
at com.intellij.junit5.JUnit5TestRunnerUtil.createSelector(JUnit5TestRunnerUtil.java:111) 
at com.intellij.junit5.JUnit5TestRunnerUtil.buildRequest(JUnit5TestRunnerUtil.java:86) 
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:46) 
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

Process finished with exit code 254 
Empty test suite. 

我不明白爲什麼這種方法不能得到解決:它編譯,它絕對有:

@Test 
void directory2bytes() { 
    testBytes = testDir.directory2bytes(); 
    for(int i = 0; i <= 10; i++) { 
     System.out.print(testBytes[i] + " ") 
    } 
    System.out.println(); 
} 

而且我已經定義了這一點:

@BeforeEach 
void setUp() { 
    testDir = new Directory() 
    testBytes = new byte[1000]; 
} 

更新:退出並重新IntelliJ IDEA的,我現在越來越導入的符號「junit的」解決不了的消息,與各種註釋「@beforeEach」以來,「afterEach」和「測試」 。

更新2:找到並修復了測試類中缺少的分號,但沒有更改。注意:在某些未進行測試的類中存在編譯錯誤,但我將測試運行配置設置爲「構建,無錯誤檢查」。這是正確的嗎?我也嘗試使用JUnit 4創建相同的測試(使用不同的類名),但這也不起作用。

下面是錯誤消息的前幾行:

Dec 07, 2016 9:51:41 AM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines 
INFO: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage] 
Internal Error occurred. 
org.junit.platform.commons.util.PreconditionViolationException: Could not load class with name: DirectoryTest 
at org.junit.platform.engine.discovery.MethodSelector.lambda$lazyLoadJavaClass$0(MethodSelector.java:154) 
+0

試圖添加這個,但顯然它沒有記錄:我現在正在Project Structure的Dependencies選項卡中顯示消息「Library JUnit5 has broken path」。 –

+0

你能否在GitHub上創建一個縮小的示例項目來演示你遇到的錯誤,而不是更多? –

+0

另外,你有沒有嘗試在IDEA中創建一個新項目,看看是否以某種方式解決了你現有項目中存在的問題? –

回答

2

在無關類中顯然編譯錯誤導致了問題。即使我指定了Build而沒有進行錯誤檢查,測試也不會運行。

大多數這些錯誤只是「缺少返回語句」,我暫時通過添加無意義的值返回來消除這些錯誤。這是一個令人討厭的浪費時間。

我只能說,錯誤信息在追蹤問題方面毫無幫助!

0

方法與TestBeforeAfter等註釋必須聲明public。您的方法正在使用默認可見性(也稱爲程序包保護),這對JUnit不可見,因此無法執行。讓他們公開並重試。

+0

謝謝,但沒有運氣(如果這些測試方法必須公開,爲什麼系統沒有爲你做這件事?)仍然得到相同的信息。 –

4

由於bug,M2不支持使用參數選擇方法。這在M3中是固定的。但是,要在IntelliJ IDEA中直接使用M3,則需要下週發佈的2016.3.1版本。有一個workaround使用M3與當前版本的IntelliJ IDEA。

+0

我使用的是2016.3.1 RC版本(適用於Mac) –

+0

我在Mac上使用IntelliJ 2016.3.6。在我的POM上,依賴項工件'junit-jupiter-api'指定的版本是'RELEASE',因爲它是由IntelliJ自動導入的。我遵循@Marc菲利普的建議,並將其更改爲'5.0.0-M3',現在一切正常。 – SylvesterAbreu