JUnit 说我的测试方法 "could not be resolved to a unique method"

JUnit says my test method "could not be resolved to a unique method"

尝试使用 IntelliJ IDEA 学习 JUnit 5。创建了一个测试 class 并尝试测试一种方法。这是消息(见第三行):

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(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" 和 "Test"。

更新 2:发现并修复了测试 class 中缺少的分号,但没有变化。注意:在我没有测试的一些 class 中存在编译错误,但我将测试 运行 配置设置为 "Build, no error check"。这是正确的吗?我还尝试使用 JUnit 4 创建相同的测试(具有不同的 class 名称),但这也不起作用。

以下是错误消息的前几行:

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[=14=](MethodSelector.java:154)

TestBeforeAfter等注释的方法必须声明public。您的方法使用默认可见性(也称为包保护),这对 JUnit 不可见,因此无法执行。让它们 public 然后再试一次。

由于 bug, M2 does not support selecting methods with parameters. This is fixed in M3. However, to use M3 in IntelliJ IDEA out of the box, you need 2016.3.1 which will be released next week. There's a workaround 将 M3 与当前版本的 IntelliJ IDEA 一起使用。

显然是非相关 类 中的编译错误导致了问题。即使我在没有错误检查的情况下指定了构建,测试也不会 运行。

这些错误中的大多数只是 "missing return statement",我通过添加一个无意义的值 return 暂时消除了这些错误。这是一种恼人的浪费时间。

我要说的是,错误消息对追踪问题毫无帮助!