vscode-java
vscode-java copied to clipboard
impossible to launch test from vscode
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.

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

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:

Is your problem solved?
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
You can use the following steps as a workaround.
- Enable the setting
java.import.generatesMetadataFilesAtProjectRootin WORKSPACE scope. - Click
Restart Now - Open the file named
.classpathwhich 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.) - Find the
classpathentrywhose 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.
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.

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.

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