2016-12-07 177 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