Java Test Runner in Visual Studio Code

Viewed 4635

I recently started to use Visual Studio Code for java development. I am having some problems with the test runner.

I can run main() in .java files without any issues, but the "run test" option does not show up in my JUnit test file. Also, no files show up in the Test runner side-bar.

Screenshot 1 Screenshot 2

This is the JUnit test file for a Student class.

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Ignore;

class Student_test {
    @Test 
    public void testGetName() {
        Student john = "John Deer"; 
        String expected = "John Deer";
        String result = john.getName();
        assertEquals(result, expected); 
    }
 }

I use the Java Test Runner extension in VS Code: https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-test

Does anyone know how I could get the test file to show the "run test" option in the file, or how I could find it in the side-bar?

1 Answers

I believe the folder you are placing your tests is wrong. For the folder to be recognized. Try to set test directory in maven

e.g.

<build>
        <testSourceDirectory>${basedir}/src/test/</testSourceDirectory>
</build>
Related