10

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.

Subrato Patnaik
  • 1,656
  • 1
  • 6
  • 25
Sally
  • 101
  • 1
  • 1
  • 3

4 Answers4

3

You don't need a .classpath file at all. Just ensure 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

After this take a look at the following script which creates the correct folder structure and places the junit jar correctly: https://github.com/siddharthbarman/Powershell/blob/master/prepare_java_project.ps1

The blog post for this is here: https://sbytestream.pythonanywhere.com/blog/VSCode-Java-JUnit

Siddharth B
  • 184
  • 7
3

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

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>
0

Got this error after updating to Java Test Runner version 0.24.2 from version 0.24 I'm using the 0.64.1 version of "Language Support for Java by Red Hat" and "Debugger for Java". After the update I couldn't see any of my junit tests in the "Test"-Tab and couldn't start them from within the IDE. Downgrading back to 0.24 & 0.64.1 resolved the issue.

Sam
  • 35
  • 5
-1

1.make sure you have installed Java Test Runner extension

2.open the .classpath file and change

<classpathentry kind="src" path="src/test/java" />

to

<classpathentry kind="src" path="src/test/java" output="build/classes/test">
   <attributes>
      <attribute name="test" value="true" />
   </attributes>
</classpathentry>

more infomation about Unit Test

Leo Zhu - MSFT
  • 12,617
  • 1
  • 3
  • 17