Skip to content

impossible to launch test from vscode #2499

Open

Description

@clembo590
Environment
  • Operating System: macos 12.0.1
  • JDK version: 11
  • Visual Studio Code version: 1.67.2
  • Java extension version: java extension pack v0.23.0
Steps To Reproduce

I have created a public repo so that the issue is easily reproducible:

step1: clone this repo:

git clone https://github.com/clembo590/issues.git --branch vscode_java_tests_no_launch

step2: run mvn clean install

step 3: "import the project in vscode"
open RoleTypeTest.java and try to execute the test from vscode: Impossible: the test is not detected.

Screenshot 2022-06-08 at 21 11 48

Activity

  1. jdneo commented on Jun 9, 2022

    @jdneo
    Collaborator

    That's because the class RoleTypeTest is put under src/main/java, which is not the place to put tests.

    image

    To provide more information, Maven has default paths to put source code and test code.

    If you do not specify any customized paths in pom.xml, test sources will be stored under src/test/java by default.

    After changing the project structure to following, testing features should work then:
    image

  2. jdneo commented on Jun 15, 2022

    @jdneo
    Collaborator

    Is your problem solved?

  3. clembo590 commented on Jun 17, 2022

    @clembo590
    Author

    Hello. Thank you for your answer. No the issue is not solved.
    If yo look at my original code and run mvn clean install you will see that the test is executed correctly.
    The question (which actually is more an issue) is: how to launch it from vscode...
    This Test can also be executed from eclipse and IntelliJ without making any change to the code.
    I would like to be able to execute the test from vscode without making any change to the code.

    I am well aware that usually test files are in the test folders, but it is not mandatory. here I want to have a full test module that has junit on its classpath (without the test scope), and contains all test classes in the main folder

  4. jdneo commented on Jun 20, 2022

    @jdneo
    Collaborator

    You can use the following steps as a workaround.

    1. Enable the setting java.import.generatesMetadataFilesAtProjectRoot in WORKSPACE scope.
    2. Click Restart Now
    3. Open the file named .classpath which you can find it at the project root. (If you cannot see it in the VS Code's file explorer, you can use system file explorer to find it.)
    4. Find the classpathentry whose path is "src/main/java", and update it to:
    <classpathentry kind="src" output="target/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="test" value="true"/>            <------ Add this line
        </attributes>
    </classpathentry>
    

    Then you should be able to see the tests in the test explorer.

  5. clembo590 commented on Jun 22, 2022

    @clembo590
    Author

    your solution works for a very simple project like the one I posted, but it does not seem to work on a more complex project:

    I posted a new branch:
    git clone https://github.com/clembo590/issues.git --branch vscode_java_tests_no_launch_multi_module
    then cd issues; code . ;

    open the file called AnotherIT.java
    you will not find the little arrow to launch the test.

    Screenshot 2022-06-22 at 17 10 20

    I also have a more complex project (that I cannot share) where vscode shows error and then the error disappear... and this I think connected to this issue as I never have issue with projects where I did not edit the .classpath manually.
    anim

    @jdneo I think the solution would be to do like in intellij or eclipse: right click on a file and then RUN AS TEST
    or automatically detect that it s a file that contains test and put a little arrow without having me to play with the classpath.

  6. jdneo commented on Jun 23, 2022

    @jdneo
    Collaborator

    My observation is that, something wrong happened for the project mdr-backend-spring-1-test. The language features like GTD, hover, are all unavailable. This might be a reason why tests cannot be found.

    Language features work fine in mdr-java-utils-test, so that we can see the test execution button in ParallelExecutionExceptionTest.

  7. clembo590 commented on Jul 29, 2022

    @clembo590
    Author

    Hi. Some time have passed sorry about that... what about this suggestion:
    @jdneo I think the solution would be to do like in intellij or eclipse: right click on a file and then RUN AS TEST
    or automatically detect that it s a file that contains test and put a little arrow without having me to play with the classpath.

  8. jdneo commented on Aug 1, 2022

    @jdneo
    Collaborator

    Yes, some changes need to be done here to make the test inference become smarter.

    Currently the extension relies on the content in .classpath, but for sure we have improvement room for that.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions