Skip to content

VS Code does not recognize junit tests #1692

Description

@siddhsql

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: #470

Activity

  1. siddhsql commented on May 16, 2024

    @siddhsql
    Author

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

  2. jdneo commented on May 17, 2024

    @jdneo
    Member

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

  3. siddhsql commented on May 17, 2024

    @siddhsql
    Author
  4. jdneo commented on May 23, 2024

    @jdneo
    Member

    Would you mind sharing the project to us?

  5. siddhsql commented on May 23, 2024

    @siddhsql
    Author
  6. jdneo commented on May 24, 2024

    @jdneo
    Member

    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.

  7. siddhsql commented on May 24, 2024

    @siddhsql
    Author
  8. diegoxxc14 commented on May 29, 2024

    @diegoxxc14

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

  9. siddhsql commented on May 30, 2024

    @siddhsql
    Author
  10. jdneo commented on May 30, 2024

    @jdneo
    Member

    siddhsql Is your problem resolved now?

  11. jdneo commented on May 30, 2024

    @jdneo
    Member

    There is an undergoing effort about support running test in gradle build server: microsoft/build-server-for-gradle#144

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

  12. aisong commented on May 30, 2024

    @aisong

    Sheng Chen (@jdneo) 我遇到了同样的问题。

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

    source action:
    WX20240530-141142@2x

    testing:
    WX20240530-141226@2x

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

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

  13. jdneo commented on May 30, 2024

    @jdneo
    Member

    aisong Could you share a sample project for your issue?

  14. 19 remaining items

  15. jdneo commented on Apr 3, 2025

    @jdneo
    Member

    我的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)
    

    我也是下载的最近的java的extension pack。

    Image 我看实际上jacoco的依赖和报错的对不上

    Please update the test runner extension (either the stable or pre-release)

  16. iHowCool commented on Apr 3, 2025

    @iHowCool

    Image it works now . GREAT !!

    add for people who use trae(the ide developed by bytedance).you can download the lastest version of extension in vscode and install manually.

    Image
  17. github-actions commented on Nov 12, 2025

    @github-actions

    Hi siddhsql, I'm an AI Support assistant here to help with your issue. While the team reviews your request, I wanted to provide some possible tips and documentation that might help you in the meantime.

    Based on the error
    Preview features enabled at an invalid source release level 21, preview can be enabled only at source level 22 VS Code fails to build your project and thus can’t discover tests. To resolve:

    • Align your Maven compiler settings with your JDK version

      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
              <!-- set to your Java version -->
              <release>21</release>
              <!-- remove if you’re not using preview features -->
              <!--
              <compilerArgs>
                <arg>--enable-preview</arg>
              </compilerArgs>
              -->
            </configuration>
          </plugin>
        </plugins>
      </build>
    • If you really need preview features:
      • Use a matching JDK (e.g. JDK 22 for Java 22 previews)
      • Or remove all --enable-preview flags and <enablePreviewFeatures> settings

    • Verify VS Code settings don’t force an invalid source level (e.g. remove or correct java.project.sourceCompatibility or any preview flags)

    • Run Maven: Update Project, then reload the window.

    • Ensure your test classes are public, in src/test/java, and annotated with org.junit.jupiter.api.Test.

    Other references with low confidence

    Click to expand

    The team will respond to your issue shortly. I hope these suggestions are helpful in the meantime. If this comment helped you, please give it a 👍. If the suggestion was not helpful or incorrect, please give it a 👎. Your feedback helps us improve!

  18. kathyrollo commented on Jun 26, 2026

    @kathyrollo

    Do not listen to the AI. This is the solution that worked for me:

    redhat-developer/vscode-java#4396 (comment)

  19. wenytang-ms commented on Jun 29, 2026

    @wenytang-ms
    Contributor

    The compatible fix requires both sides to be updated:

    redhat.java 1.55.0 or newer
    Test Runner for Java 0.46.0 or newer

    redhat.java 1.55.0 contains the JDT-LS/JDT fix, and Test Runner for Java 0.46.0 contains the matching fixes for test discovery and bundled JUnit/ASM dependencies. I verified this combination with a plain Eclipse Java project using junit-platform-console-standalone-1.13.4.jar, and the JUnit 5 tests were discovered correctly.

  20. desmondc9 commented on Sep 22, 2026

    @desmondc9

    Minimal reproduction — I can reproduce this deterministically with a bare-bones Maven project, and the trigger is a single flag: --enable-preview in maven-compiler-plugin.

    Environment

    • eclipse.jdt.ls 1.60.0 (202606262232), java-test plugin 0.43.1 (loaded via initOptions.bundles)
    • JDK 21.0.2 running the LS, matching the project's <release>21</release>
    • Observed from Neovim (nvim-jdtls) calling the same LSP commands VS Code uses — confirming this is server-side and client-independent

    Steps

    1. Create a project with this pom.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.example</groupId>
      <artifactId>preview-repro</artifactId>
      <version>1.0.0</version>
      <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
      </properties>
      <dependencies>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter</artifactId>
          <version>5.11.4</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>21</source>
              <target>21</target>
              <release>21</release>
              <compilerArgs>
                <arg>--enable-preview</arg>
              </compilerArgs>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

    and src/test/java/com/example/MiniTest.java:

    package com.example;
    
    import org.junit.jupiter.api.Test;
    
    public class MiniTest {
        @Test
        public void testOne() {
            System.out.println("one");
        }
    
        @Test
        public void testTwo() {
            System.out.println("two");
        }
    }
    1. Open the project and wait for the Maven import to finish. vscode.java.test.findTestTypesAndMethods returns [] — an empty result, not an error. No test lenses, nothing in the Test Explorer.

    2. Remove only the <compilerArgs><arg>--enable-preview</arg></compilerArgs> block (change nothing else) and re-import. Tests are discovered immediately, with correct method-level items (testLevel = 6).

    What still works with preview enabled (which is what makes this confusing to diagnose):

    • vscode.java.test.resolvePath correctly reports the test source folder/package (testLevel = 4)
    • java.project.isTestFile returns true
    • The test classpath resolves fine (junit-jupiter 5.11.4 present) and the test file shows 0 problems
    • Hover on @Test resolves the annotation and shows its Javadoc

    Where it breaks (from inspecting com.microsoft.java.test.plugin-0.43.1 and the org.eclipse.jdt.junit.core bundle shipped with jdt.ls): TestSearchUtils.findTestTypesAndMethods → JUnit5TestSearcher.isTestClass → JUnit5TestFinder.isTest(type) → ASTParser.setProject(...) + createBindings(...) yields no usable binding when the project options have enablePreviewFeatures=enabled, so isTest returns false for every type, the searcher gets filtered out, and the handler returns an empty list. This matches the error already quoted in this issue ("Preview features enabled at an invalid source release level 21, preview can be enabled only at source level 22") — ECJ only accepts preview at its latest supported source level, and on this code path the rejection silently degrades to "no tests found" instead of surfacing an error.

    vscode.java.test.findTestPackagesAndTypes returns [] for the same reason, so the Test Explorer stays empty project-wide.

    Since --enable-preview combined with a fixed --release is a documented, supported Maven configuration, test discovery shouldn't silently return empty in this setup. A cleaner failure (e.g., surfacing the ECJ preview error) would already make this much easier to hit-diagnose. Thanks!


    Update (2026-09-22): re-tested with the latest published components on both sides — the Maven path is still broken.

    Following wenyt (@wenytang-ms)'s comment above, I re-ran the minimal repro with the newest available pieces:

    • eclipse.jdt.ls 1.61.0 (1.61.0.202609031315, the 2026-09-03 milestone — two months after the fix shipped in redhat.java 1.55.0)
    • Test Runner 0.46.0 (current marketplace build; note that both marketplace builds I checked bundle server plugin com.microsoft.java.test.plugin 0.43.1 — the plugin jar itself does not appear to have been republished)

    Result:

    Scenario vscode.java.test.findTestTypesAndMethods
    same project, --enable-preview removed (control) ✅ tests discovered on first attempt after import (testLevel = 6 items)
    with <compilerArgs><arg>--enable-preview</arg></compilerArgs> ❌ returns [], retried 6× over ~90s after initialization job finished

    So the fix combination appears to cover the scenario it was verified with — a plain Eclipse-format project where preview is enabled via Eclipse project settings — but not Maven projects, where m2e maps --enable-preview from maven-compiler-plugin <compilerArgs> into the JDT project options.

    To help narrow it down: with preview enabled, vscode.java.test.resolvePath still reports the test package correctly (testLevel = 4), java.project.isTestFile returns true, the file reconciles with 0 problems, and hover on @Test resolves with full Javadoc — only the ASTParser-binding-based test item resolution silently returns nothing.

  21. wenytang-ms commented on Sep 23, 2026

    @wenytang-ms
    Contributor

    Thanks for the detailed minimal reproduction and the retest. I traced the server-side failure and opened #1938 with a fix for the test-discovery path.

    With JDT's project options at source=21, preview=enabled, the discovery AST reports PreviewFeaturesNotAllowed (the JDT version tested accepts preview only for its latest source level). In findTestTypesAndMethods, TypeDeclaration.resolveBinding() then returns null, and the extension returns [] before it reaches JUnit5TestFinder. The PR retries discovery using a parser-local preview-disabled option when the source level is older than JDT's latest, without changing project settings or the --enable-preview test launch argument. It also covers project-wide discovery/nested tests and surfaces a diagnostic if bindings still cannot be resolved.

    I verified the behavior in an isolated VS Code extension host (JDK 21, JDT LS 1.61.0, Test Runner 0.46.0) with JDT preview explicitly enabled; the control without preview also works. One caveat: on my machine the Maven compilerArgs in your POM alone did not turn on the JDT preview project option, so I have not independently reproduced that Maven-to-JDT import step. The new Tycho regression test also could not run locally because the target-platform build fails with missing JDT types. The PR is open for review; I am leaving this issue open until the fix is validated against your Maven setup and released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions