Cannot use JUNIT Testing on VSCode

Gabriel 0 Reputation points
2025-02-11T11:32:50.27+00:00

Hello Everyone

Im new to Java and VSCode and im trying to use JUNIT for my current classes on Java, but im not able to use JUnit. I have installed all the official extensions (The Java extensions, and the java extension pack), and I run this guide here:

https://code.visualstudio.com/docs/java/java-testing

But I still have the same issues. I created a very simple test file:

Java File:

public class Greet {

    public String says() {

        return "Hello";

    }

}

public class Greet {

    public String says() {

        return "Hello";

    }

}
```Test File:

```java
import org.junit.

*

;

public class GreetTest {

    @Test

    public void test() {

        Greet gre = new Greet();

    }

}





import org.junit.*;

public class GreetTest {

    @Test

    public void test() {

        Greet gre = new Greet();

    }

}
```But it always creates the same two issues: "org.junit Package does not exist" is the error i receive in the code and when I switch to the Test-Tab it does not show me the Test-File to run the test-file with.

What am I missing here? I also found this post, but that did not help: [https://stackoverflow.com/questions/56244560/vscode-not-finding-unit-tests](https://stackoverflow.com/questions/56244560/vscode-not-finding-unit-tests) 

  

Thank you for your help.

  

Cheers, 

  

Gabe
Developer technologies Visual Studio Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Tianyu Sun-MSFT 34,436 Reputation points Microsoft External Staff
    2025-02-12T05:48:10.88+00:00

    Hello @Gabriel ,

    Can you confirm that you have clicked the Testing option on the left => click Enable Java Tests => and select to install JUnit?

    I believe that you have followed the document to install the JUnit, then the "hamcrest-core-1.3.jar" and the "junit-4.13.2.jar" should be installed.

    User's image

    If the issue persists, please try to follow/check the following complete steps:

    1. Install VS Code, Extension Pack for Java, and choose to install JDK after the extension finishes installing.
    2. Create a new empty folder, create a sub folder in this new folder and name it src. Put the .java files into the src folder.

    User's image

    User's image

    1. Launch VS Code, File, Open Folder, then open that new folder.

    User's image

    User's image

    1. Click to open the GreetTest.java file and wait for the Testing option appears. Once the Testing option appears, click it, click Enable Java Tests, select JUnit, click the button to run the test.

    User's image

    User's image

    User's image

    User's image

    If you follow the above steps, but the error persists, please try to reinstall VS Code, reinstall JDK(uninstall it from Control Panel), and reinstall the extension then test again.

    Best Regards,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Gabriel 0 Reputation points
    2025-02-14T06:15:20.53+00:00

    Hello Tianyu

    I unfortunately still receive the same error message after the reinstallation:

    User's image

    And yes, both java files are being created:

    User's image

    KInd regards,

    Gabriel

    0 comments No comments

  3. 行者 人间 0 Reputation points
    2025-04-13T12:37:47.95+00:00

    Your java-extension doesn't know your jar packages even if they lie in your project. Expected behavior is like:
    User's image

    Under "JAVA PROJECTS" > "Referenced Libraries", there are jar packages (ignore gurobi-12.0.1.jar, that is my personal-purpose jar).

    Conventionally, java-extension (actually extension "Project Manager for Java") know where your jar packages are by folder name "lib", so try rename your "test-lib" to "lib". If it doesn't work, you can manage the dependencies by clicking the "+" icon or the "-" icon on the "Referenced Libraries" node, or you can just directly drag your jar libraries to the node "Referenced Libraries". For more information: dependencies-for-unmanaged-folder.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.