Wednesday, May 4, 2022

Java Test with Visual Studio Code

By Torian Harris


This tutorial will guide you through how to write and test a Java program using Visual Studio Code. It will also cover how to push the program to a Repository and automatically run tests by building a pipeline.


The first step is to install Visual Studio Code which you can download here: Download Visual Studio Code - Mac, Linux, Windows. If you do not have Java already installed, you can find it here: Download Java for Windows

Once you open the application, click the Extensions icon (Ctrl + Shift + X) on the left then type in "Extension Pack of Java" and install it by clicking the install button.




Open View > Command Palette (Ctrl + Shift + P) and type in ">Maven: Create Maven Project”, click it then click “maven-archetype-quickstart” and version “1.4." Afterward, name the group ID and artifact ID and save the project to a folder. Press Enter to proceed through the Terminal prompts then, open your newly created project.





Replace the code in App.Java with the following code:




package com.example;
public class  App {
    public static void main(String[] args) {
    // adds two ints
    public static int sum(int a, int b) {
      int sum = a + b;
      return sum;
    // calculates an additional expression
    public static int evaluate(String expression) {
      int sum = 0;
      for (String summand: expression.split("\\+"))
        sum += Integer.valueOf(summand);
      return sum;




Next, click Run on the App class. This will open a Terminal that will display the results of the sum and evaluate functions.




Now that we have a working project, let us set up some tests to validate the functions. Replace the code in AppTest.Java with the following code:





package com.example;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AppTest 
    public void sumTest() {
        int sum = App.sum(1,2);
        assertEquals(3, sum);
    public void evaluateTest() {
        int sum = App.evaluate("2+3+4");
        assertEquals(9, sum);





Click the play button next to the AppTest class to run the tests. The Testing Window will open showing the progress and results of the tests.





Now, we have functioning testing for our Java program. The next thing to do is to create a repository and build our pipeline. Navigate to your existing organization at​ or create a new organization if you don’t already have one and click New Project. On the left, click Repos to find your repo URL. Copy it, then in Visual Studio Code, open the Command Palette, type in “>Git: Clone” and confirm. From here paste in the URL from your repo and save it locally. Copy all files from your previous project into the new project structure. Then in the Source Control window, commit and push your changes to the server.


Lastly, it is time to build our pipeline. Head to the Pipelines tab in Azure DevOps and click “New Pipeline.” Select Azure Repos Git, select your repository, then “starter pipeline.” In the YAML file, change the value of “mavenPomFile:” to “’pom.xml’” and on the right side clear out the Solution name. Click “Save and Run” and confirm to start the build process. If you click on the queued Job, you will get to see the build process in action. Once that is done, head back to your pipeline and click “Tests” to see the results of your run.

Congratulations! You have built a Java program and a test pipeline using Visual Studio Code.

If you are interested in learning more about Java testing features using Visual Studio Code, check out this post:

Visual Studio Code + Java Feb 2022 updates

Posted at