Issue
I try to use JUnit tests in a Java project in VS Code.
My setup
1) JDK 17
2) Via the Extension Pack for Java I have a its extensions installed including the following:
3) I went to the Testing pane and downloaded (via the wizard) the testing JARs hamcrest-core-1.3.jar
and junit-4.13.2.jar
into the lib
directory.
4) I made sure in Classpath Configuration the two JARs are listed under Referenced Libraries.
5) The project contains a main unqualified App.java with a main method and a qualified package like org.mydomain.calculations
. The method(s) which I want to test are in the qualified package!
What I did then
In a class of the qualified package I right-click and choose from the context menu Source Actions..., then Generate Tests.... From the appearing project chooser I select the current project -- and then I get the error ballon "Cannot find a valid classpath entry to generate tests".
This confuses me, because I did add the JUnit JAR to the classpath.
What else can I do to make generating tests in the qualified package work?
Solution
Please install all extensions in the Extension Pack for Java and use Clean the java language server workspace
in the command palette to clean up the Java workspace.
Follow the steps below to implement a minimal test example in vscode.
Open the command palette(Ctrl+Shift+P) and create a java project
Add an AppTest.java file
Select Enable Java Tests in the Testing panel
select JUit
It will automatically download third-party packages. The tests can then be run.
Answered By - JialeDu
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)