Robert Lewis
Robert Lewis

Reputation: 1907

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

Trying to learn JUnit 5 with IntelliJ IDEA. Created a test class and trying to test one method. Here's the message (see third line):

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.

I can't understand why the method can't be resolved: It compiles and it's definitely there:

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

And I have defined this:

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

Update: quitting and relaunching IntelliJ IDEA, I am now getting messages that the imported symbol "junit" can't be resolved, along with the various annotations "@beforeEach", "afterEach" and "Test".

Update 2: found and fixed a missing semicolon in test class, but no change. Note: there are compile errors in some of the classes that I'm not testing, but I have the test Run configuration set to "Build, no error check". Is this right? I have also tried to create the same test (with a different class name) using JUnit 4, but that doesn't work either.

Here are first few lines of error message:

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)

Upvotes: 4

Views: 4943

Answers (3)

Robert Lewis
Robert Lewis

Reputation: 1907

Evidently compile errors in noninvolved classes were causing the problems. Even though I had specified Build without error check, the tests wouldn't run.

Most of these errors were simply "missing return statement", which I eliminated for the time being by adding a meaningless value return. This was an annoying waste of time.

All I have to say is, the error messages were fantastically UNhelpful in tracking down the problem!

Upvotes: 4

Marc Philipp
Marc Philipp

Reputation: 1908

Due to a 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 to use M3 with the current version of IntelliJ IDEA.

Upvotes: 4

nasukkin
nasukkin

Reputation: 2540

Methods annotated with Test, Before, After, etc. must be declared public. Your methods are using the default visibility (also known as package-protected), which aren't visible to JUnit, and thus cannot be executed. Make them public and try again.

Upvotes: 1

Related Questions