Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit in Visual Studio Code

I'm having all sorts of trouble running JUnit in Visual Studio Code. I have had two main problems that do not co-exist in each all the projects I've created.

Problem 1: After creating and running a Java project, if I attempt to add a JUnit test class, the program cannot find the class/test. I should see an option to run or debug my test, but no such option pops up. Also when I click on the test Explorer tab, I don't see the test class listed, but it is in the project folder.

Problem 2: I have had some success with importing a project first developed in eclipse into VS code. This is not really ideal. When doing this the issue I have is that VS Code has troubles with the JUnit imports. Sometimes it does not recognize the annotation @Test and will attempt to solve the import troubles by having me at the import within the program, (Example: When using assertSame method I would have to write: org.junit.Assert.assertEquals("stringName", v1.getName);

Has anyone had similar problems, or does anyone have some insight on how to run a java project without a maven or another project manager? I'm sure there is a way to make this work, but I feel like I'm missing the obvious.

like image 448
Sally Avatar asked Jun 19 '19 14:06

Sally


People also ask

Does Visual Studio support JUnit?

The extension supports the following test frameworks: JUnit 4 (v4. 8.0+) JUnit 5 (v5.

How do I run unit tests in Visual Studio?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).


1 Answers

To do Java Unit Tests in Visual Studio Code I suggest you to have in place the following requirements

  • JDK 11
  • Language Support for Java by Red Hat
  • Debugger for Java
  • Java Test Runner.

Once you have that and your newly created Java project, you should know always separate Production code from Test code. For that, simply create a test folder in the same location as src.

Once you have you have that folder, create a SomethingTest.java file with something like

import static org.junit.Assert.fail;

import org.junit.Test;

public class NgramTest {
    
    @Test 
    void test() {
        fail("Not yet implemented");
    }

}

Run Test JUnit

When you click "Run Test", you should get a "Not yet implemented" message

JUnit result after running


If you're not able to run that and get a result, can be from at least two different reasons

1.

The import org.junit cannot be resolved

To fix it, if the project uses Maven to manage the dependencies, then make sure you've got Maven installed (How to install Maven in Windows 10?). Then make sure you ran mvn install or mvn package, as mentioned here.

2.

SomethingTest.java is not on the classpath of the project PROJECT_NAME, only syntax errors are reported

To fix it, add that folder to .classpath present in the root of the project

<classpathentry kind="src" output="target/test-classes" path="test">
    <attributes>
        <attribute name="optional" value="true"/>
        <attribute name="maven.pomderived" value="true"/>
        <attribute name="test" value="true"/>
    </attributes>
</classpathentry>
like image 121
Tiago Martins Peres Avatar answered Sep 28 '22 00:09

Tiago Martins Peres