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

impossible to launch test from vscode

Open clembo590 opened this issue 3 years ago • 8 comments

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

clembo590 avatar Jun 08 '22 19:06 clembo590

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

jdneo avatar Jun 09 '22 07:06 jdneo

Is your problem solved?

jdneo avatar Jun 15 '22 00:06 jdneo

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

clembo590 avatar Jun 17 '22 07:06 clembo590

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.

jdneo avatar Jun 20 '22 03:06 jdneo

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.

clembo590 avatar Jun 22 '22 15:06 clembo590

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.

jdneo avatar Jun 23 '22 01:06 jdneo

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.

clembo590 avatar Jul 29 '22 18:07 clembo590

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.

jdneo avatar Aug 01 '22 05:08 jdneo