vscode-java-test icon indicating copy to clipboard operation
vscode-java-test copied to clipboard

VS Code does not recognize junit tests

Open siddhsql opened this issue 1 year ago • 35 comments

I am following the steps at this page to run Junit5 tests with VS Code and Maven. I have these extensions installed:

image image image

and my pom.xml has:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>    
    <scope>test</scope>
</dependency>        

I have put my test classes in src/test/java. but VS Code does not detect JUNIT5 tests. the green "lenses" or "buttons" do not show up next to the tests. In one of the logs I could see:

Preview features enabled at an invalid source release level 21, preview can be enabled only at source level 22; code: 2098258
image

and not sure if it is because of this.

related: https://github.com/Microsoft/vscode-java-test/issues/470

siddhsql avatar May 16 '24 18:05 siddhsql

I did some search and came across mention of a .classpath file. I do not see such file anywhere in my project.

siddhsql avatar May 16 '24 18:05 siddhsql

@siddhsql What if you run Java: Clean Java Language Server Workspace

jdneo avatar May 17 '24 01:05 jdneo

i have done that many times. same result.

On Thu, May 16, 2024 at 6:59 PM Sheng Chen @.***> wrote:

@siddhsql https://github.com/siddhsql What if you run Java: Clean Java Language Server Workspace

— Reply to this email directly, view it on GitHub https://github.com/microsoft/vscode-java-test/issues/1692#issuecomment-2116491531, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6NWEKY7MB2YKSRO2JZGRNTZCVQBVAVCNFSM6AAAAABH2WQIISVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJWGQ4TCNJTGE . You are receiving this because you were mentioned.Message ID: @.***>

siddhsql avatar May 17 '24 16:05 siddhsql

Would you mind sharing the project to us?

jdneo avatar May 23 '24 08:05 jdneo

i am not able to do that. it is a big project with many modules.

On Thu, May 23, 2024 at 1:59 AM Sheng Chen @.***> wrote:

Would you mind sharing the project to us?

— Reply to this email directly, view it on GitHub https://github.com/microsoft/vscode-java-test/issues/1692#issuecomment-2126589593, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6NWEK2UQEV3AEEWPHRV4L3ZDWVXHAVCNFSM6AAAAABH2WQIISVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRWGU4DSNJZGM . You are receiving this because you were mentioned.Message ID: @.***>

siddhsql avatar May 23 '24 14:05 siddhsql

If there are .settings, .classpath, .project files located under your project folder.

Please try to close your vscode, remove the above three files and open your vscode.

jdneo avatar May 24 '24 06:05 jdneo

I don't have any of these files. I have a settings.json under .vscode and it has following in it:

{
    "java.configuration.updateBuildConfiguration": "automatic",
    "java.compile.nullAnalysis.mode": "disabled",
    "java.test.config": {
        "vmArgs": ["--enable-preview", "--add-modules",
"jdk.incubator.vector", "--add-opens",
        "java.base/java.lang.invoke=ALL-UNNAMED",
        "--add-opens",
        "java.base/jdk.internal.misc=ALL-UNNAMED",
        "--add-opens",
        "java.base/sun.security.util=ALL-UNNAMED",
        "--add-opens",
        "java.base/jdk.internal.ref=ALL-UNNAMED",
        "--add-opens", "java.base/java.lang.reflect=ALL-UNNAMED",
        "--add-opens", "java.base/java.nio.channels.spi=ALL-UNNAMED",
        "--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED", "--add-opens",
"java.base/sun.nio.cs=ALL-UNNAMED", "--add-opens",
"java.base/java.nio.charset=ALL-UNNAMED",
        "--add-opens", "java.base/java.lang.ref=ALL-UNNAMED",
"--add-opens", "java.base/java.nio=ALL-UNNAMED", "--add-opens",
"java.base/java.nio.channels=ALL-UNNAMED",
        "--add-opens", "java.base/java.io=ALL-UNNAMED", "--add-opens",
"java.base/jdk.internal.loader=ALL-UNNAMED", "--add-opens",
"java.base/java.util.regex=ALL-UNNAMED",
        "--add-opens", "java.base/java.security=ALL-UNNAMED",
"--add-opens", "java.base/java.net=ALL-UNNAMED", "--add-opens",
"java.base/sun.nio.fs=ALL-UNNAMED",
        "--add-opens", "java.base/java.lang=ALL-UNNAMED", "--add-opens",
"java.base/java.util.concurrent=ALL-UNNAMED", "--add-opens",
"java.base/java.util.concurrent.atomic=ALL-UNNAMED",
        "--add-opens", "java.base/java.util.concurrent.locks=ALL-UNNAMED",
"--add-opens", "java.base/java.util=ALL-UNNAMED"
        ]
    },
    "git.ignoreLimitWarning": true
}

I also tested on a Windows machine and there too VS Code does not recognize junit tests - no green button is displayed beside the tests.

On Thu, May 23, 2024 at 11:54 PM Sheng Chen @.***> wrote:

If there are .settings, .classpath, .project files located under your project folder.

Please try to close your vscode, remove the above three files and open your vscode.

— Reply to this email directly, view it on GitHub https://github.com/microsoft/vscode-java-test/issues/1692#issuecomment-2128712647, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6NWEKYAUVMEG72H2EX766TZD3P2HAVCNFSM6AAAAABH2WQIISVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRYG4YTENRUG4 . You are receiving this because you were mentioned.Message ID: @.***>

siddhsql avatar May 24 '24 16:05 siddhsql

Hi, I solved this problem in JAVA PROJECTS, Clean Workspace. Then the green buttons are displayed in tests. image

diegoxxc14 avatar May 29 '24 22:05 diegoxxc14

it does not work for me though. i have tried cleaning the workspace many times.

On Wed, May 29, 2024 at 3:56 PM Diego Cuenca @.***> wrote:

Hi, I solved this problem in JAVA PROJECTS, Clean Workspace. Then the green buttons are displayed in tests. image.png (view on web) https://github.com/microsoft/vscode-java-test/assets/41596984/038b7bad-f5c6-4349-b1cf-d3783e3ba431

— Reply to this email directly, view it on GitHub https://github.com/microsoft/vscode-java-test/issues/1692#issuecomment-2138385350, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6NWEKYFJLUPEF4GY4E2YG3ZEZMIZAVCNFSM6AAAAABH2WQIISVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMZYGM4DKMZVGA . You are receiving this because you were mentioned.Message ID: @.***>

siddhsql avatar May 30 '24 00:05 siddhsql

@siddhsql Is your problem resolved now?

jdneo avatar May 30 '24 01:05 jdneo

There is an undergoing effort about support running test in gradle build server: https://github.com/microsoft/build-server-for-gradle/pull/144

Once Once it's done, I'll check how to integrate that into the test runner extension.

jdneo avatar May 30 '24 01:05 jdneo

@jdneo 我遇到了同样的问题。

执行mvn test-compile -f 可以编译成功。但是在vscode里面找不到测试类。 更加有问题的是,ide似乎不识别test下面的java类,语法错误不会提示。

source action: WX20240530-141142@2x

testing: WX20240530-141226@2x

清除目录、clear workspace都重复了很多次,都不解决问题。

能麻烦看下可能是什么问题导致?

aisong avatar May 30 '24 06:05 aisong

@aisong Could you share a sample project for your issue?

jdneo avatar May 30 '24 06:05 jdneo

@aisong Could you share a sample project for your issue? @jdneo 业务系统的代码,不好直接发这里呀。🥲

aisong avatar May 30 '24 06:05 aisong

我觉得是test目录的识别有问题,没有被正确识别为java sources。

虽然classpath的配置是正确的: WX20240530-150118@2x

但因为某种原因,ide没有把src/test/java识别为源文件。

查看执行文件,里面只有classes,没有包含test-classes: WX20240530-150636@2x

aisong avatar May 30 '24 07:05 aisong

@aisong Have you set java.import.maven.disableTestClasspathFlag

jdneo avatar May 30 '24 07:05 jdneo

@aisong Have you set java.import.maven.disableTestClasspathFlag

not set.

settings:

{ "java.configuration.maven.globalSettings": "/Users/ddxx/dev/maven/apache-maven-3.8.2/conf/settings.xml", "redhat.telemetry.enabled": false, "java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx1G -Xms100m -javaagent:"/Users/ddxx/.vscode/extensions/vscjava.vscode-lombok-1.0.1/server/lombok.jar"", "java.configuration.runtimes": [ { "name": "JavaSE-17", "path": "/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home", // "path": "/Library/Java/JavaVirtualMachines/jdk1.8.0_341.jdk/Contents/Home" } ], "java.autobuild.enabled": false, "terminal.integrated.env.osx": { "FIG_NEW_SESSION": "1" }, "workbench.colorTheme": "Default Light Modern", "codeium.enableConfig": { "*": true }, "editor.minimap.enabled": false, "sonarlint.rules": {

    "Web:ItemTagNotWithinContainerTagCheck": {
        "level": "off"
    },
    "Web:ImgWithoutAltCheck": {
        "level": "off"
    }
},
"sonarlint.output.showVerboseLogs": true,
"sonarlint.focusOnNewCode": true,
"eslint.lintTask.enable": true,
"sonarlint.output.showAnalyzerLogs": true,
"[vue]": {
    "editor.defaultFormatter": "Vue.volar"
},
"sonarlint.connectedMode.project": {},
"sonarlint.connectedMode.connections.sonarqube": [],
"maven.terminal.customEnv": [],
"sonarlint.pathToNodeExecutable": "/Users/ddxx/Dev/DevEnvs/node/node-v18.20.2-darwin-arm64/bin/node",
"editor.stickyScroll.enabled": false,
"java.editor.reloadChangedSources": "auto"

}

aisong avatar May 30 '24 07:05 aisong

no my problem is not solved. i don't use gradle. i am using maven with junit 5. thanks.

siddhsql avatar May 30 '24 15:05 siddhsql

this is an unrelated question but does anyone know where does VS Code store the compiled classes? there is a target folder but its meant for use by Maven. Does VS Code overwrite the files in that folder with its compiled code? and is it possible to designate a different folder for vs code so that it does not interfere with maven?

siddhsql avatar May 30 '24 15:05 siddhsql

Does VS Code overwrite the files in that folder with its compiled code?

Yes

is it possible to designate a different folder for vs code so that it does not interfere with maven?

This is a known issue: https://github.com/redhat-developer/vscode-java/issues/314

jdneo avatar May 31 '24 01:05 jdneo

@jdneo 我终于找到我这里的问题出在哪里。但不知道为什么会这样。

在我的pom中引入了一个本地仓库的依赖,如果去掉它,ide会正常,如果引入它就会出现问题。 我打开了trace日志,截取了当在pom中引入这个依赖后出现的错误日志,请见附件。 vscode-java.log

能否从日志中判断出具体是什么问题?

aisong avatar May 31 '24 01:05 aisong

@aisong, thank you! Tracked at https://github.com/microsoft/vscode-java-test/issues/1693. Will investigate.

jdneo avatar May 31 '24 02:05 jdneo

@aisong, thank you! Tracked at #1693. Will investigate.

好的。看起来是读取依赖的信息时有报错。是依赖包中的文件缺失了什么必要信息么。还没找到解决办法。

aisong avatar May 31 '24 02:05 aisong

redhat-developer/vscode-java#314

this seems like a serious issue to me and am surprised its not fixed even after 7 years.

siddhsql avatar May 31 '24 18:05 siddhsql

@siddhsql I also came across the same problem, but, I resolved this problem by adding a .classpath file. Sometimes, VSC cannot detect src\java\test as test source folder automatically( i don't know why ). You can enhance it by adding a .classpath file to tell vsc which folder should regarded as test source folder.

ezrealliu avatar Nov 30 '24 08:11 ezrealliu

Hi Simon, thanks. could you elaborate please with more details?

On Sat, Nov 30, 2024 at 12:07 AM SimonLiu @.***> wrote:

@siddhsql https://github.com/siddhsql I also came across the same problem, but, I resolved this problem by adding a .classpath file. Sometimes, VSC cannot detect src\java\test as test source folder automatically( i don't know why ). You can enhance it by adding a .classpath file to tell vsc which folder should regarded as test source folder.

— Reply to this email directly, view it on GitHub https://github.com/microsoft/vscode-java-test/issues/1692#issuecomment-2508880208, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6NWEK2H2DBT7B3ZSEUFSFT2DFW37AVCNFSM6AAAAABSYI3FNOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMBYHA4DAMRQHA . You are receiving this because you were mentioned.Message ID: @.***>

siddhsql avatar Nov 30 '24 19:11 siddhsql

Image Try this.

qtycc-cc avatar Jan 20 '25 04:01 qtycc-cc

Image Try this.

try了,木用

caohenghui avatar Apr 02 '25 06:04 caohenghui

我也是突然扫描不到test了

Image

CoderL98 avatar Apr 03 '25 01:04 CoderL98

我的Test图标也没有,我发现启动的时候有如下的报错。

Image

[Error - 15:48:16] 2025年4月3日 15:48:15 FrameworkEvent ERROR org.osgi.framework.BundleException: Could not resolve module: com.microsoft.java.test.plugin [117] Unresolved requirement: Require-Bundle: org.jacoco.core; bundle-version="0.8.12" -> Bundle-SymbolicName: org.jacoco.core; bundle-version="0.8.12.202403310830" org.jacoco.core [116] Unresolved requirement: Import-Package: org.objectweb.asm; version="[9.7.0,9.8.0)"

at org.eclipse.osgi.container.Module.start(Module.java:495)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel$2.run(ModuleContainer.java:2111)
at org.eclipse.osgi.internal.framework.EquinoxContainerAdaptor$1$1.execute(EquinoxContainerAdaptor.java:146)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:2102)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:2042)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:2004)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1916)
at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:341)

iHowCool avatar Apr 03 '25 07:04 iHowCool