How i can install junit 5 on VSCode

Viewed 11897

i'm trying to learn unit testing in java and JUnit frame but i'm on how i can and use junit on visual studio code. should i create maven project to that. i tried to import that but it does not work.

2 Answers

First of all, you need to have correctly installed the Java Extention Pack in your VSCode.

Secondly, you need to download a .jar file of your preferred junit5 version. You can visit this link https://search.maven.org/artifact/org.junit.platform/junit-platform-console-standalone/1.7.0-M1/jar and download it as jar file.

After you have downloaded that file you need to add the following lines in your settings.json:

"java.project.referencedLibraries": [
    "lib/**/*.jar",
    "C:\\PathToFile\\junit-platform-console-standalone-1.6.0.jar"
]

Insert the path to the file you have downloaded.

If you have performed all the above steps correctly, you will be able to run unit tests in VSCode. Also, make sure that your test class and the methods you are testing are both public. It is advised to use the following naming convention for test class (*Test) as some users in GitHub reported problems when it was not used. Regardless that, it is considered good practice.

  1. Install the Java Extention Pack Extension in your VSCode. Be sure the one by Microsoft.
  2. Install Test Runner for Java Extension in your VSCode.
  3. Keep your program directory like this: enter image description here
  4. Ctrl + SHIFT + p: settings.json > Preference: Open Default Settings(JSON)
{
    "java.project.sourcePaths": ["src"],
    "java.project.outputPath": "bin",
    "java.project.referencedLibraries": [
        "lib/**/*.jar"
    ]
} 
  1. Click Test img on the left side VScode, and Enable it: enter image description here
  2. Code your Test: enter image description here
Related