Home > front end >  When trying to generate Java Tests in VS Code I get "Cannot find a valid classpath entry to gen
When trying to generate Java Tests in VS Code I get "Cannot find a valid classpath entry to gen

Time:08-01

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:

  • Language Support for Java by Red Hat
  • Debugger for Java
  • Java Test Runner

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?

CodePudding user response:

Please install all extensions in the enter image description here

Follow the steps below to implement a minimal test example in vscode.

  1. Open the command palette(Ctrl Shift P) and create a java project

    enter image description here

  2. Add an AppTest.java file

    enter image description here

  3. Select Enable Java Tests in the Testing panel

    enter image description here

  4. select JUit

    enter image description here

It will automatically download third-party packages. The tests can then be run.

enter image description here

  • Related