Testing Java with Visual Studio Code
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.
Overview
The 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:
- Run/Debug test cases
- Customize test configurations
- View test report
- View tests in Testing Explorer
Requirements
- JDK (version 1.8 or later)
- Visual Studio Code (version 1.59.0 or later)
- Extension Pack for Java
Project Setup
Note: If you have already setup your Java test framework in your project, you can skip to the Features section.
Enable testing and adding test framework JARs to your project
Starting 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:
Note: Currently this feature only supports unmanaged folders that do not contain any testing dependencies.
JUnit 4
Maven
Add following configuration into your pom.xml
:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>(YOUR_JUNIT_VERSION)</version>
<scope>test</scope>
</dependency>
Gradle
Make sure following lines are added in your build.gradle
:
plugins {
java
}
dependencies {
testImplementation('junit:junit:(YOUR_JUNIT_VERSION)')
}
Unmanaged folder
If your project does not use any build tools, you can enable JUnit 4 via the Testing Explorer or by manually downloading the following JARs and adding them to the project classpath (via setting java.project.referencedLibraries
, check Dependency management for more information):
You can check the official JUnit Wiki for more information about how to setup JUnit 4.
JUnit 5
The 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 folder
If your project does not use any build tools, you can enable JUnit 5 via the Testing Explorer or by manually including the junit-platform-console-standalone JAR in the project classpath (via setting java.project.referencedLibraries
, check Dependency management for more information).
TestNG
Maven
Add following configuration into your pom.xml
:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>(YOUR_TESTNG_VERSION)</version>
<scope>test</scope>
</dependency>
Gradle
Make sure following lines are added in your build.gradle
:
plugins {
java
}
dependencies {
testImplementation('org.testng:testng:(YOUR_TESTNG_VERSION)')
}
Unmanaged folder
If your project does not use any build tools, you can enable TestNG via the Testing Explorer or by manually downloading the following JARs and adding them to the project classpath (via setting java.project.referencedLibraries
, check Dependency management for more information):
Features
Run/Debug test cases
The 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.