There's Sublime Text, Notepad++ and then there's VSCode. VSCode has been coming along nicely since its introduction in November 18, 2015. Microsoft has been steadily adding support for various languages. I recently started using it for Java.
I sort of got stuck when running JUnit tests from VSCode. I did find a few articles explaining how to get it working. Coming from a C# background I always have a slight mental block when working with Java. Anyway, if you following the tutorials on the WWW about this you should be OK. If you're NOT, then here is a super easy way to get you some help. First of all make sure you have the following plugins installed:
- Java Extention Pack from Microsoft
- Java Dependency Viewer from Microsoft
- Debugger for Java from Microsoft
- Java Test Runner from Microsoft
- Language Support for Java(TM) by Red Hat
Here is a PowerShell script which works like this. You call a PowerShell script named prepare_java_project.ps1 specifying the path where the project folders will be created along with option to generate sample .java code and JUnit tests.
First check if your machine is configured to run unsigned PowerShell scripts:
If you get the text Unrestricted, you can directly run the prepare_java_project. If not, then execute the following PowerShell command:
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
- The first argument is the path where the project folder is to be created.
- The second argument is the name of the Project folder. All generated files and folders will be placed within this folder.
- The third parameter decides if sample source code and unit tests are to be genered.
prepare_java_project.ps1 d:\temp MyJavaProject $true
The following project folders are created:
The script will even open VSCode with the folder loaded for you. Now, all you have to do is save the workspace at the root folder which would be d:\temp\MyJavaProject as per the code given above. And... you're done. If you now open the GreetingTests.java file, you should get the 'Run test | Debug Test' codelens options. As you'll see in the image below, we have run the test and it passed.
Click the button below to get to download the script.Get the Code