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
290 stars 124 forks source link

VS Code does not recognize junit tests #1692

Open siddhsql opened 2 months ago

siddhsql commented 2 months ago

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 commented 2 months ago

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

jdneo commented 2 months ago

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

siddhsql commented 2 months ago

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: @.***>

jdneo commented 1 month ago

Would you mind sharing the project to us?

siddhsql commented 1 month ago

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: @.***>

jdneo commented 1 month ago

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.

siddhsql commented 1 month ago

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: @.***>

diegoxxc14 commented 1 month ago

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

siddhsql commented 1 month ago

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: @.***>

jdneo commented 1 month ago

@siddhsql Is your problem resolved now?

jdneo commented 1 month ago

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.

aisong commented 1 month ago

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

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

source action: WX20240530-141142@2x

testing: WX20240530-141226@2x

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

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

jdneo commented 1 month ago

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

aisong commented 1 month ago

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

aisong commented 1 month ago

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

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

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

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

jdneo commented 1 month ago

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

aisong commented 1 month ago

@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"

}

siddhsql commented 1 month ago

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

siddhsql commented 1 month ago

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?

jdneo commented 1 month ago

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

aisong commented 1 month ago

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

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

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

jdneo commented 1 month ago

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

aisong commented 1 month ago

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

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

siddhsql commented 1 month ago

redhat-developer/vscode-java#314

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