Testing Java in Visual Studio Code is enabled by the Test Runner for Java extension. It's a lightweight extension to run and debug Java test cases. Show OverviewThe extension supports the following test frameworks:
The Test Runner for Java works with the Language Support for Java™ by Red Hat and Debugger for Java extensions to provide the following features:
Requirements
Install the Extension Pack for Java Project Setup
Enable testing and adding test framework JARs to your projectStarting with Test Runner for Java version 0.34.0, you can enable a test framework for your unmanaged folder project (a project without any build tools) with just a few steps in the Testing Explorer:
JUnit 4MavenAdd following configuration into your
GradleMake sure following lines are added in your
Unmanaged folderIf your project does not use any build tools, you can enable JUnit 4 via the or by manually downloading the following JARs and adding them to the project classpath (via setting
JUnit 5The JUnit 5 team provides a collection of sample projects with different build tools. Check the junit5-sample repository if your project uses Maven or Gradle as your build tool. Unmanaged folderIf your project does not use any build tools, you can enable JUnit 5 via the or by manually including the junit-platform-console-standalone JAR in the project classpath (via setting TestNGMavenAdd following configuration into your
GradleMake sure following lines are added in your
Unmanaged folderIf your project does not use any build tools, you can enable TestNG via the or by manually downloading the following JARs and adding them to the project classpath (via setting
FeaturesRun/Debug test casesThe Test Runner for Java extension will generate shortcuts (the green play button) on the left side of the class and method definition. To run the target test cases, select the green play button. You can also right-click on it to see more options. Testing ExplorerThe Testing Explorer is a tree view to show all the test cases in your workspace. You can select the beaker button on the left-side Activity bar of Visual Studio Code to open it. You can also run/debug your test cases and view their test results from there. Customize test configurationsSometimes you may want to customize the configuration to run your test cases. To achieve this, you can add the configuration into your workspace settings under the section: 1.Currently the supported configurations are:
More details can be found on the vscode-java-test Wiki. View test resultsAfter running/debugging the test cases, the state of the related test items will be updated in both editor decorations and the Testing Explorer. You can trigger the command Test: Peek Output to peek the results view. You can select the links in the stack trace to navigate to the source location. Generate testsThe extension provides features to help you scaffold test cases. You can find the entry in the editor context menu. Select Source Action... and then choose Generate Tests.... If you trigger this source action from your main source code (test subject), you will be asked the test class's fully qualified name and the methods you want to test. The extension will then generate the test code for you: If you trigger the source action from your test source code, you will be asked which kinds of test methods you want to add. Including the lifecycle methods and the test method: Test navigationThe extension provides features to help you navigate between your tests and test subjects. If your source code is contained in 0 or 1, you can find the entry named Go to Test or Go to Test Subject in the editor context menu:You can also find the command in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) by searching for Java: Go to Test. VS Code testing commandsThere are other testing commands (for example, Run Tests in Current File) that can be found by searching for 'Test:' in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). VS Code testing settingsThere are VS Code settings specific to testing that can be found by searching for 'testing' in the Settings editor (⌘, (Windows, Linux Ctrl+,)). FAQIf you meet any problem when using the extension, you can review the FAQ and our issue list to check if there is an answer to your problem. Contributing and feedbackIf you are interested in providing feedback or contributing directly to the code base, please read Contributing to Test Runner for Java, which covers the following: |