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+)
Browse other questions tagged spring-boot visual-studio-code or ask your own question. The Overflow Blog How often do people actually copy and paste from Stack Overflow? Podcast 331: One in four visitors to Stack Overflow copies code. Featured on Meta Stack Overflow for Teams is now free for up to 50 users, forever.
Active Oldest Votes. You will only be able to remote debug with vs code, so a simple command will be mvnDebug spring-boot:run, which will do the same thing as mvn spring-boot:run but add these options: -Xdebug -Xrunjdwp:transport=dtsocket,address=8000,server=y,suspend=y. Then you can attach from vs code, a sample launch.json looks like. Azure Spring Cloud for Visual Studio Code (Preview) Azure Spring Cloud provides a managed service that lets you run microservices on Azure using Spring Boot with no code changes. Use the Azure Spring Cloud extension for VS Code to quickly create, manage and deploy apps to an Azure Spring Cloud instance.
Note: More information about the test frameworks can be found at JUnit and TestNG.
The Java Test Runner 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 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.
Quickstart
Make sure you have the below tools available already
- JDK (version 11 or later)
- VS Code (version 1.23.0 or later)
You may refer to Java Extensions to set up the environment.
Note: More information about JDK can be found at supported Java versions.
Getting Started for JUnit 5
Please refer to Getting Started from the JUnit 5 official documentation.
Note: You can use junit-platform-console-standalone.jar in projects that manually manage their dependencies similar to the plain-old JAR known from JUnit 4.
Getting Started for JUnit 4
Please refer to Download and Install from the JUnit 4 official documentation.
Getting Started for TestNG
Please refer to TestNG Docs from the TestNG official documentation.
Run|Debug CodeLens
When you open a project with test cases from supported frameworks, Test Runner will be activated and 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 Run Test and Debug Test shortcuts (also known as CodeLens) above the class and method definition. Select them to 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.
Test Explorer
- 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
Spring Boot Visual Studio Code Maven
- 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:
java.test.config
.
Note: More details can be found in Run with Configuration.
View Test Report
- After running/debugging the test cases, the test report is opened automatically. You can also see the final results in the status bar, by clicking on it to show the Test Report.
- You can also click the ✔️ or ❌ mark in the CodeLens to open the Test Report.
- For a quick peek of the test status or results, you can see them in the Test Explorer directly.
- You can navigate to the source location of the target test case by clicking the navigate button.
JUnit5 Support
Spring Boot Visual Studio Code 2020
The JUnit 5 support covers frequently used annotations such as @DisplayName
, @ParameterizedTest
, @TestFactory
, @TestTemplate
, and @Nested
. It also supports meta-annotations and composed annotations.
FAQ
If you meet any problem when using the extension, you can refer to the FAQ to check if there is an answer to your problem.
More Information
Visit the GitHub repository of the Java Test Runner for more details on commands and settings.
Next steps
Visual Studio Code Spring Boot Profile
Read on to find out about:
- Debugging - Find out how to debug your Java project with VS Code.
- Java Extensions - Learn about more useful Java extensions for VS Code.
Visual Studio Code is an ideal lightweight development environment for Spring Boot application developers and there are several useful VS Code extensions including:
If you run into any issues when using the features below, you can contact us by clicking the Report an issue button below.
Prerequisites
A working Java environment with essential extensions installed is needed, including:
- Java Development Kit (JDK), version 11 or later.
- Apache Maven, version 3.0 or later.
For more details, please refer to Java Tutorial
Note: More information about JDK can be found at supported Java versions.
Create the project
The Spring Initializr extension allows you to search for dependencies and generate new Spring Boot projects.
To install, launch VS Code and from the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)), search for vscode-spring-initializr
.
Once you have the extension installed, open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and type Spring Initializr
to start generating a Maven or Gradle project and then follow the wizard.
Edit the project
The Spring Initializr extension allows you to edit dependencies after generating a new Spring Boot project.
Navigate to your pom.xml
file and right-click to select Edit starters
. The Command Palette will show the dependencies you already have beginning with a √
. You can search for other dependencies you want to add to your project. Or you can click on the existing dependencies to remove them.
Develop the application
The Spring Boot Tools extension includes rich language support for working with Spring Boot application.properties
, application.yml
, and .java
files.
Getting Started for JUnit 5
Please refer to Getting Started from the JUnit 5 official documentation.
Note: You can use junit-platform-console-standalone.jar in projects that manually manage their dependencies similar to the plain-old JAR known from JUnit 4.
Getting Started for JUnit 4
Please refer to Download and Install from the JUnit 4 official documentation.
Getting Started for TestNG
Please refer to TestNG Docs from the TestNG official documentation.
Run|Debug CodeLens
When you open a project with test cases from supported frameworks, Test Runner will be activated and 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 Run Test and Debug Test shortcuts (also known as CodeLens) above the class and method definition. Select them to 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.
Test Explorer
- 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
Spring Boot Visual Studio Code Maven
- 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:
java.test.config
.
Note: More details can be found in Run with Configuration.
View Test Report
- After running/debugging the test cases, the test report is opened automatically. You can also see the final results in the status bar, by clicking on it to show the Test Report.
- You can also click the ✔️ or ❌ mark in the CodeLens to open the Test Report.
- For a quick peek of the test status or results, you can see them in the Test Explorer directly.
- You can navigate to the source location of the target test case by clicking the navigate button.
JUnit5 Support
Spring Boot Visual Studio Code 2020
The JUnit 5 support covers frequently used annotations such as @DisplayName
, @ParameterizedTest
, @TestFactory
, @TestTemplate
, and @Nested
. It also supports meta-annotations and composed annotations.
FAQ
If you meet any problem when using the extension, you can refer to the FAQ to check if there is an answer to your problem.
More Information
Visit the GitHub repository of the Java Test Runner for more details on commands and settings.
Next steps
Visual Studio Code Spring Boot Profile
Read on to find out about:
- Debugging - Find out how to debug your Java project with VS Code.
- Java Extensions - Learn about more useful Java extensions for VS Code.
Visual Studio Code is an ideal lightweight development environment for Spring Boot application developers and there are several useful VS Code extensions including:
If you run into any issues when using the features below, you can contact us by clicking the Report an issue button below.
Prerequisites
A working Java environment with essential extensions installed is needed, including:
- Java Development Kit (JDK), version 11 or later.
- Apache Maven, version 3.0 or later.
For more details, please refer to Java Tutorial
Note: More information about JDK can be found at supported Java versions.
Create the project
The Spring Initializr extension allows you to search for dependencies and generate new Spring Boot projects.
To install, launch VS Code and from the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)), search for vscode-spring-initializr
.
Once you have the extension installed, open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and type Spring Initializr
to start generating a Maven or Gradle project and then follow the wizard.
Edit the project
The Spring Initializr extension allows you to edit dependencies after generating a new Spring Boot project.
Navigate to your pom.xml
file and right-click to select Edit starters
. The Command Palette will show the dependencies you already have beginning with a √
. You can search for other dependencies you want to add to your project. Or you can click on the existing dependencies to remove them.
Develop the application
The Spring Boot Tools extension includes rich language support for working with Spring Boot application.properties
, application.yml
, and .java
files.
Spring Boot Visual Studio Codes
The extension supports quick navigate through source code, smart code completions, quick access to running apps, live application information, and code templates. Similar code completion and validation features are also available for .properties
and .yml
files.
Run the application
In addition to click F5 to run your application, there's another convenient extension Spring Boot Dashboard with which you can view and manage all available Spring Boot projects in your workspace as well as quickly start, stop, or debug your project.
Visual Studio Code Spring Boot Project
Next steps
Visual Studio Code Spring Boot App
- To deploy your web app, see the Deploy a Java Application to Azure tutorial.
- To containerize a web app and deploy as a Docker container, check out the Working with Docker.
- To learn more about Java Debugging features, see Java Debugging Tutorial.