microsoft / vscode-java-test

Run and debug Java test cases in Visual Studio Code.
https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-test
Other
297 stars 131 forks source link

Run Test for Java class generates "Cannot get method declaration" error #1721

Closed raffian closed 3 months ago

raffian commented 3 months ago

A strange one indeed....

Setup

I'm developing with Java 21 and JUnit 5 using @CvsSource and @ParameterizedTest. I use Run Test for executing tests, no problem - all is good. Spent a few hours today refactoring a test class and had to comment out a few redundant tests, after doing so the test class looked something like this:

package com.zone.twilight;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class TestParameterized {
    //FORMER_TEST_PARAMS
    //@CsvSource(
    //    {",must not be null",
    //     "0L,size must be between 1 and 999999999"})
    //@ParameterizedTest

    /**
     * [RequestContext.sessionId]
     * Tests all invalid scenarios for requestContext.logonId.
     */
    @CsvSource(delimiter = '|', textBlock = """
        -1          | size must be between 1 and 999999999
         """)
    @ParameterizedTest
    void whenInvalidReqCtxSessionId(Long __INPUT, String __EXPECTED) throws Exception {
        assert(true);
    }
}

Problem

After making those harmless edits, all tests in the class stopped working. Click Run Test now for any test does nothing except display pop-up error ...

Cannot get method declaration of methodwhenInvalidReqCtxSessionId Source: Test Runner for Java

Solution?

It took hours to figure out but the culprit appears to be those commented lines //@ParameterizedTest and //@CsvSource preceding the actual test. Delete those comments, Run Test, no more error. Grammar parser error or bad interpreter is what this feels like to me but what do I know.

class TestParameterized {
    /**
     * [RequestContext.sessionId]
     * Tests all invalid scenarios for requestContext.logonId.
     */
    @CsvSource(delimiter = '|', textBlock = """
        -1          | size must be between 1 and 999999999
         """)
    @ParameterizedTest
    void whenInvalidReqCtxSessionId(Long __INPUT, String __EXPECTED) throws Exception {
        assert(true);
    }

Specs

VSCode: 1.91.1 Java Runtime: OpenJDK 64-Bit Server VM Corretto-21.0.3.9.1 OS: Windows 10/22H2 Extensions

pom.xml

[Trace - 8:04:06 PM] Sending request 'workspace/executeCommand - (1080)'.
Params: {
    "command": "vscode.java.test.junit.argument",
    "arguments": [
        "{\"projectName\":\"executor\",\"testLevel\":6,\"testKind\":0,\"testNames\":[\"=executor/src\\\\/test\\\\/java=/optional=/true=/=/maven.pomderived=/true=/=/test=/true=/<com.zone.twilight{TestParameterized.java[TestParameterized~whenInvalidReqCtxSessionId~QLong;~QString;\"]}"
    ]
}

[Trace - 8:04:07 PM] Received notification 'window/logMessage'.
Params: {
    "type": 1,
    "message": "Jul 31, 2024, 8:04:07 PM failed to resolve the classpath.\nCannot get method declaration of methodwhenInvalidReqCtxSessionId\norg.eclipse.core.runtime.CoreException: Cannot get method declaration of methodwhenInvalidReqCtxSessionId\r\n\tat com.microsoft.java.test.plugin.launchers.JUnitLaunchConfigurationDelegate.addTestItemArgs(JUnitLaunchConfigurationDelegate.java:167)\r\n\tat com.microsoft.java.test.plugin.launchers.JUnitLaunchConfigurationDelegate.parseParameters(JUnitLaunchConfigurationDelegate.java:137)\r\n\tat com.microsoft.java.test.plugin.launchers.JUnitLaunchConfigurationDelegate.getJUnitLaunchArguments(JUnitLaunchConfigurationDelegate.java:89)\r\n\tat com.microsoft.java.test.plugin.launchers.JUnitLaunchUtils.resolveLaunchArgument(JUnitLaunchUtils.java:102)\r\n\tat com.microsoft.java.test.plugin.handler.TestDelegateCommandHandler.executeCommand(TestDelegateCommandHandler.java:55)\r\n\tat org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:230)\r\n\tat org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)\r\n\tat org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:220)\r\n\tat org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$4(JDTLanguageServer.java:604)\r\n\tat org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:87)\r\n\tat java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)\r\n\tat java.base/java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:483)\r\n\tat java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)\r\n\tat java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)\r\n\tat java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)\r\n\tat java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)\r\n\tat java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)\r\n"
}

[Error - 8:04:07 PM] Jul 31, 2024, 8:04:07 PM failed to resolve the classpath.
Cannot get method declaration of methodwhenInvalidReqCtxSessionId
org.eclipse.core.runtime.CoreException: Cannot get method declaration of methodwhenInvalidReqCtxSessionId
    at com.microsoft.java.test.plugin.launchers.JUnitLaunchConfigurationDelegate.addTestItemArgs(JUnitLaunchConfigurationDelegate.java:167)
    at com.microsoft.java.test.plugin.launchers.JUnitLaunchConfigurationDelegate.parseParameters(JUnitLaunchConfigurationDelegate.java:137)
    at com.microsoft.java.test.plugin.launchers.JUnitLaunchConfigurationDelegate.getJUnitLaunchArguments(JUnitLaunchConfigurationDelegate.java:89)
    at com.microsoft.java.test.plugin.launchers.JUnitLaunchUtils.resolveLaunchArgument(JUnitLaunchUtils.java:102)
    at com.microsoft.java.test.plugin.handler.TestDelegateCommandHandler.executeCommand(TestDelegateCommandHandler.java:55)
    at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:230)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
    at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:220)
    at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$4(JDTLanguageServer.java:604)
    at org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:87)
    at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)
    at java.base/java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:483)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)

[Trace - 8:04:07 PM] Received response 'workspace/executeCommand - (1080)' in 829ms.
Result: {
    "status": 1,
    "errorMessage": "Cannot get method declaration of methodwhenInvalidReqCtxSessionId"
}
jdneo commented 3 months ago

Thank you for providing the detailed information for the bug. This should be fixed in the latest pre-release.

raffian commented 3 months ago

@jdneo

My pleasure, and thank you for the quick turnaround. My colleague is very active with VSCode projects and speaks highly of your team - now I see why 👍