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 132 forks source link

Coverage Testing Not Working #1710

Closed spragunr closed 4 months ago

spragunr commented 4 months ago

Coverage testing isn't working for me. When I run with coverage, the progress monitor spins indefinitely and no coverage information is provided. The test results are displayed correctly if I run without coverage.

This is in an unmanaged folder with a single HelloWorld file and a single JUnit5 test file. No workspace settings are provided.

I'm seeing the following error in the extension host logs:

2024-07-16 11:57:47.238 [error] Error: Error while analyzing /home/spragunr/.config/Code/User/workspaceStorage/ac7fe07f62529f2e22a176555b6754b7/redhat.java/jdt_ws/test_java_6709e97f/bin/lib/junit-platform-console-standalone-1.11.0-M2.jar@org/junit/platform/commons/util/ModuleUtils.class with JaCoCo 0.8.12.202403310830/dbfb6f2.
    at /home/spragunr/.vscode/extensions/redhat.java-1.32.0-linux-x64/dist/extension.js:2:1106298
    at ie (/home/spragunr/.vscode/extensions/redhat.java-1.32.0-linux-x64/dist/extension.js:2:1106592)
    at /home/spragunr/.vscode/extensions/redhat.java-1.32.0-linux-x64/dist/extension.js:2:1101376
    at Immediate.<anonymous> (/home/spragunr/.vscode/extensions/redhat.java-1.32.0-linux-x64/dist/extension.js:2:1101396)
    at process.processImmediate (node:internal/timers:478:21) java.execute.workspaceCommand {"value":"redhat.java","_lower":"redhat.java"}

The only extensions I have installed are from the "Extension Pack For Java".

vscode-java-test Extension version: 0.41.1 VS Code Version: Version: 1.91.1 Commit: f1e16e1e6214d7c44d078b1f0607b2388f29d729 Date: 2024-07-09T22:08:12.169Z Electron: 29.4.0 ElectronBuildId: 9728852 Chromium: 122.0.6261.156 Node.js: 20.9.0 V8: 12.2.281.27-electron.0 OS: Linux x64 5.15.0-116-generic

jdneo commented 4 months ago

Would you mind sharing the sample project to us?

spragunr commented 4 months ago

@jdneo Here it is:

test_java.zip

jdneo commented 4 months ago

Thank you! I can reproduce it. Full stacktrace:

java.io.IOException: Error while analyzing C:\Users\....
    at org.jacoco.core.analysis.Analyzer.analyzerError(Analyzer.java:163)
    at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:135)
    at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:158)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:195)
    at org.jacoco.core.analysis.Analyzer.analyzeZip(Analyzer.java:267)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:198)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:228)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:223)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:223)
    at com.microsoft.java.test.plugin.coverage.CoverageHandler.getCoverageDetail(CoverageHandler.java:91)
    at com.microsoft.java.test.plugin.handler.TestDelegateCommandHandler.executeCommand(TestDelegateCommandHandler.java:85)
    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(Unknown Source)
    at java.base/java.util.concurrent.CompletableFuture$Completion.exec(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Can't add different class with same name: org/junit/platform/commons/util/ModuleUtils
    at org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:106)
    at org.jacoco.core.analysis.Analyzer$1.visitEnd(Analyzer.java:100)
    at org.objectweb.asm.ClassVisitor.visitEnd(ClassVisitor.java:395)
    at org.jacoco.core.internal.flow.ClassProbesAdapter.visitEnd(ClassProbesAdapter.java:100)
    at org.objectweb.asm.ClassReader.accept(ClassReader.java:749)
    at org.objectweb.asm.ClassReader.accept(ClassReader.java:425)
    at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:117)
    at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:133)
    ... 21 more
jdneo commented 4 months ago

It turns out that for some reason, the junit jar is also included in the bin folder, and be analyzed by jacoco. (Usually, jacoco should only analyze user's source code.)

To avoid this exception, one thing you can do is explicitly declare the source, lib, output location of your project. You can do the following steps to achieve that:

  1. Create .vscode/settings.json in your project root.
  2. Create src and bin folder in your project root.
  3. In .vscode/settings.json, add:
    "java.test.config": {
    "coverage": {
    "appendResult": false,
    }
    },
    "java.project.sourcePaths": [
    "src"
    ],
    "java.project.referencedLibraries": [
    "lib/junit-platform-console-standalone-1.11.0-M2.jar"
    ],
    "java.project.outputPath": "bin"
  4. Move all your java files into src

Then everything should be fine.