Testing Java with Visual Studio Code
Testing Java in Visual Studio Code is enabled by the Java Test Runner extension. It's a lightweight extension to run and debug Java test cases. The extension supports the following test frameworks:
- JUnit 4 (v4.8.0+)
- JUnit 5 (v5.1.0+)
- TestNG (v6.8.0+)
- Run/Debug test cases
- Customize test configurations
- View test report
- View tests in Test Explorer
- Show test logs
If you run into any issues when using the features below, you can contact us by clicking the Report an issue button below.
Make sure you have the below tools available already
- JDK (version 1.8.0 or later)
- VS Code (version 1.23.0 or later)
- Language Support for Java by Red Hat
- Debugger for Java
You may refer to Java Extensions to set up the environment.
Once the test runner is activated, you will find Run|Debug on the CodeLens of your test functions. Click on the CodeLens to run the individual test case. You can also access and run a group of test cases from the Test Explorer. For more information on debugging test cases, see Debugging Java. Here's a brief session with TestNG:
Run/Debug Test Cases
- The extension will generate
Debug Testshortcuts (also known as CodeLens) above the class and method definition. Simply click on them will start running or debugging the target test cases.
Note: If you cannot see the CodeLens in your editor, please refer to this issue comment as a workaround.
- The Test Explorer is the place to show all the test cases in your project. You can also run/debug your test cases from here.
- Click the node in the Test Explorer will navigate to the location of the source code.
Note: If the Test Explorer is empty, please refer to this issue comment as a workaround.
Customize Test Configurations
- Sometimes you may want to customize the configuration for running the test cases. To achieve this, you can add it into your workspace settings under the section:
Note: More details can be found here.
View Test Report
- After running/debugging the test cases, the status bar will show the final results. Simply click on it to show the Test Report.
- You can also click the ✔️ or ❌ mark in CodeLens to open the Test Report.
- You can navigate to the source location of the target test case by clicking the navigate button.
The JUnit 5 support also covers frequently used annotations such as
Read on to find out about: