Java Test with Visual Studio Code

Published May 04 2022 07:08 AM 813 Views

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.

Microsoft_Testing_Team_5-1651671208773.png

 

 

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.

 

Microsoft_Testing_Team_6-1651672613960.png

 

 

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

 

package com.example;
 
public class  App {
    public static void main(String[] args) {
      System.out.println(sum(1,2));
      System.out.println(evaluate("2+3+4"));
    }
    
    // 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.

test_VSC_5.jpg

 

 

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 
{
    @Test
    public void sumTest() {
        int sum = App.sum(1,2);
        assertEquals(3, sum);
    }
 
    @Test
    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.

 

Microsoft_Testing_Team_7-1651672991359.png

 

 

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 https://azure.microsoft.com/en-us/services/devops/​ 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

%3CLINGO-SUB%20id%3D%22lingo-sub-3302401%22%20slang%3D%22en-US%22%3EJava%20Test%20with%20Visual%20Studio%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3302401%22%20slang%3D%22en-US%22%3E%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3EBy%20Torian%20Harris%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3EThis%20tutorial%20will%20guide%20you%20through%20how%20to%20write%20and%20test%20a%20Java%20program%20using%20Visual%20Studio%20Code.%20It%20will%20also%20cover%20how%20to%20push%20the%20program%20to%20a%20Repository%20and%20automatically%20run%20tests%20by%20building%20a%20pipeline.%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3EThe%20first%20step%20is%20to%20install%20Visual%20Studio%20Code%20which%20you%20can%20download%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fcode.visualstudio.com%2FDownload%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDownload%20Visual%20Studio%20Code%20-%20Mac%2C%20Linux%2C%20Windows%3C%2FA%3E.%20If%20you%20do%20not%20have%20Java%20already%20installed%2C%20you%20can%20find%20it%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fwww.java.com%2Fen%2Fdownload%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EDownload%20Java%20for%20Windows%3C%2FA%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3EOnce%20you%20open%20the%20application%2C%20click%20the%20Extensions%20icon%20(Ctrl%20%2B%20Shift%20%2B%20X)%20on%20the%20left%20then%20type%20in%20%22Extension%20Pack%20of%20Java%22%20and%20install%20it%20by%20clicking%20the%20install%20button.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22Microsoft_Testing_Team_5-1651671208773.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F369115i90F777D94B0DE6A7%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Microsoft_Testing_Team_5-1651671208773.png%22%20alt%3D%22Microsoft_Testing_Team_5-1651671208773.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3EOpen%20View%20%26gt%3B%20Command%20Palette%20(Ctrl%20%2B%20Shift%20%2B%20P)%20and%20type%20in%20%22%26gt%3BMaven%3A%20Create%20Maven%20Project%E2%80%9D%2C%20click%20it%20then%20click%20%E2%80%9Cmaven-archetype-quickstart%E2%80%9D%20and%20version%20%E2%80%9C1.4.%22%20Afterward%2C%20name%20the%20group%20ID%20and%20artifact%20ID%20and%20save%20the%20project%20to%20a%20folder.%20Press%20Enter%20to%20proceed%20through%20the%20Terminal%20prompts%20then%2C%20open%20your%20newly%20created%20project.%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22Microsoft_Testing_Team_6-1651672613960.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F369126i874E9F90E23B0705%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Microsoft_Testing_Team_6-1651672613960.png%22%20alt%3D%22Microsoft_Testing_Team_6-1651672613960.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3EReplace%20the%20code%20in%20App.Java%20with%20the%20following%20code%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epackage%20com.example%3B%0A%20%0Apublic%20class%20%20App%20%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20System.out.println(sum(1%2C2))%3B%0A%20%20%20%20%20%20System.out.println(evaluate(%222%2B3%2B4%22))%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20adds%20two%20ints%0A%20%20%20%20public%20static%20int%20sum(int%20a%2C%20int%20b)%20%7B%0A%20%20%20%20%20%20int%20sum%20%3D%20a%20%2B%20b%3B%0A%20%20%20%20%20%20return%20sum%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20calculates%20an%20additional%20expression%0A%20%20%20%20public%20static%20int%20evaluate(String%20expression)%20%7B%0A%20%20%20%20%20%20int%20sum%20%3D%200%3B%0A%20%20%20%20%20%20for%20(String%20summand%3A%20expression.split(%22%5C%5C%2B%22))%0A%20%20%20%20%20%20%20%20sum%20%2B%3D%20Integer.valueOf(summand)%3B%0A%20%20%20%20%20%20return%20sum%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3ENext%2C%20click%20Run%20on%20the%20App%20class.%20This%20will%20open%20a%20Terminal%20that%20will%20display%20the%20results%20of%20the%20sum%20and%20evaluate%20functions.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22test_VSC_5.jpg%22%20style%3D%22width%3A%20989px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F369128i0B3D55E65E54BB63%2Fimage-dimensions%2F989x517%3Fv%3Dv2%22%20width%3D%22989%22%20height%3D%22517%22%20role%3D%22button%22%20title%3D%22test_VSC_5.jpg%22%20alt%3D%22test_VSC_5.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3ENow%20that%20we%20have%20a%20working%20project%2C%20let%20us%20set%20up%20some%20tests%20to%20validate%20the%20functions.%20Replace%20the%20code%20in%20AppTest.Java%20with%20the%20following%20code%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epackage%20com.example%3B%0A%20%0Aimport%20static%20org.junit.Assert.assertEquals%3B%0Aimport%20org.junit.Test%3B%0A%20%0Apublic%20class%20AppTest%20%0A%7B%0A%20%20%20%20%40Test%0A%20%20%20%20public%20void%20sumTest()%20%7B%0A%20%20%20%20%20%20%20%20int%20sum%20%3D%20App.sum(1%2C2)%3B%0A%20%20%20%20%20%20%20%20assertEquals(3%2C%20sum)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%40Test%0A%20%20%20%20public%20void%20evaluateTest()%20%7B%0A%20%20%20%20%20%20%20%20int%20sum%20%3D%20App.evaluate(%222%2B3%2B4%22)%3B%0A%20%20%20%20%20%20%20%20assertEquals(9%2C%20sum)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%20color%3A%20black%3B%22%3EClick%20the%20play%20button%20next%20to%20the%20AppTest%20class%20to%20run%20the%20tests.%20The%20Testing%20Window%20will%20open%20showing%20the%20progress%20and%20results%20of%20the%20tests.%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22Microsoft_Testing_Team_7-1651672991359.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F369130i9A9514A3816FCB6F%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Microsoft_Testing_Team_7-1651672991359.png%22%20alt%3D%22Microsoft_Testing_Team_7-1651672991359.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20font-family%3A%20'Courier%20New'%3B%20font-size%3A%2010.0pt%3B%20color%3A%20navy%3B%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3ENow%2C%20we%20have%20functioning%20testing%20for%20our%20Java%20program.%20The%20next%20thing%20to%20do%20is%20to%20create%20a%20repository%20and%20build%20our%20pipeline.%20Navigate%20to%20your%20existing%20organization%20at%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fservices%2Fdevops%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fservices%2Fdevops%2F%3C%2FA%3E%20or%20create%20a%20new%20organization%20if%20you%20don%E2%80%99t%20already%20have%20one%20and%20click%20New%20Project.%20On%20the%20left%2C%20click%20Repos%20to%20find%20your%20repo%20URL.%20Copy%20it%2C%20then%20in%20Visual%20Studio%20Code%2C%20open%20the%20Command%20Palette%2C%20type%20in%20%E2%80%9C%26gt%3BGit%3A%20Clone%E2%80%9D%20and%20confirm.%20From%20here%20paste%20in%20the%20URL%20from%20your%20repo%20and%20save%20it%20locally.%20Copy%20all%20files%20from%20your%20previous%20project%20into%20the%20new%20project%20structure.%20Then%20in%20the%20Source%20Control%20window%2C%20commit%20and%20push%20your%20changes%20to%20the%20server.%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3ELastly%2C%20it%20is%20time%20to%20build%20our%20pipeline.%20Head%20to%20the%20Pipelines%20tab%20in%20Azure%20DevOps%20and%20click%20%E2%80%9CNew%20Pipeline.%E2%80%9D%20Select%20Azure%20Repos%20Git%2C%20select%20your%20repository%2C%20then%20%E2%80%9Cstarter%20pipeline.%E2%80%9D%20In%20the%20YAML%20file%2C%20change%20the%20value%20of%20%E2%80%9CmavenPomFile%3A%E2%80%9D%20to%20%E2%80%9C%E2%80%99pom.xml%E2%80%99%E2%80%9D%20and%20on%20the%20right%20side%20clear%20out%20the%20Solution%20name.%20Click%20%E2%80%9CSave%20and%20Run%E2%80%9D%20and%20confirm%20to%20start%20the%20build%20process.%20If%20you%20click%20on%20the%20queued%20Job%2C%20you%20will%20get%20to%20see%20the%20build%20process%20in%20action.%20Once%20that%20is%20done%2C%20head%20back%20to%20your%20pipeline%20and%20click%20%E2%80%9CTests%E2%80%9D%20to%20see%20the%20results%20of%20your%20run.%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3ECongratulations!%20You%20have%20built%20a%20Java%20program%20and%20a%20test%20pipeline%20using%20Visual%20Studio%20Code.%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3EIf%20you%20are%20interested%20in%20learning%20more%20about%20Java%20testing%20features%20using%20Visual%20Studio%20Code%2C%20check%20out%20this%20post%3A%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Feducator-developer-blog%2Fvisual-studio-code-java-feb-2022-updates%2Fba-p%2F3177491%3Fsearch-action-id%3D414404253120%26amp%3Bsearch-result-uid%3D3177491%22%20target%3D%22_blank%22%3EVisual%20Studio%20Code%20%2B%26nbsp%3B%3CSPAN%3EJava%3C%2FSPAN%3E%26nbsp%3BFeb%202022%20updates%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-3302401%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Microsoft_Testing_Team_0-1651675003792.png%22%20style%3D%22width%3A%20321px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F369153iD465681423F0A475%2Fimage-dimensions%2F321x174%3Fv%3Dv2%22%20width%3D%22321%22%20height%3D%22174%22%20role%3D%22button%22%20title%3D%22Microsoft_Testing_Team_0-1651675003792.png%22%20alt%3D%22Microsoft_Testing_Team_0-1651675003792.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin-top%3A%200pt%3B%20margin-bottom%3A%208pt%3B%20font-family%3A%20Calibri%3B%20font-size%3A%2011.0pt%3B%22%3EThis%20tutorial%20will%20guide%20you%20through%20how%20to%20write%20and%20test%20a%20Java%20program%20using%20Visual%20Studio%20Code.%20It%20will%20also%20cover%20how%20to%20push%20the%20program%20to%20a%20Repository%20and%20automatically%20run%20tests%20by%20building%20a%20pipeline.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3302401%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ELoad%20Test%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EQA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EQuality%20Assurance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETesting%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EVisual%20Studio%20Test%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎May 04 2022 07:44 AM