Caution: VS Code guides section is a work-in-progress, containing content contributed by students.
This tutorial covers the basics of using VS Code’s JUnit.
Ensure you have followed our guide on Preparing VS Code for Java.
As JUnit is a third-party library, you need to add support to it specifically in your project. Given below is how you can do that using the . While it is possible to add JUnit to your project without Gradle, we strongly recommend using Gradle as it can make things easier in the long run.
If you have not done that yet, follow the Gradle Tutorial to add Gradle support to the project first.
This tutorial assumes you are using Gradle to manage JUnit.
Update the build.gradle
file to include JUnit as a dependency. Here are the relevant lines that needs to be in the build.gradle
(change the version number as necessary):
First, ensure the Java plugin is included:
plugins {
id 'java'
}
Next add the following JUnit dependencies to the dependencies
block, to tell which JUnit libraries to be used:
dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.0'
}
Finally, tell Gradle that JUnit is to be used as the testing tool (and to configure a few aspects of how Gradle handles JUnit tests), as follows:
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
showExceptions true
exceptionFormat "full"
showCauses true
showStackTraces true
showStandardStreams = false
}
}
Restart VSCode after updating the build.gradle
file, to ensure the changes take effect.
Here are the conventions to follow, when using JUnit in a Gradle project:
[project root]\src\test\java\
folder.Todo.java
can be tested by TodoTest.java
), and put it in a package to match (reason: if packages are matched, the test class can access package-private members of the target class).seedu.duke.Todo
: src\main\java\seedu\duke\Todo.java
seedu.duke.TodoTest
: src\test\java\seedu\duke\TodoTest.java
Add a test class, while following the conventions given earlier in this page. If you don't follow those conventions, Gradle will not be able to find your test class. For example, if you have a class src\main\java\seedu\duke\Todo.java
, you can add a test class src\
test\java\seedu\duke\
TodoTest.java. Here's some sample code:
package seedu.duke; //same package as the class being tested
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DukeTest {
@Test
public void dummyTest(){
assertEquals(2, 2);
}
@Test
public void anotherDummyTest(){
assertEquals(4, 4);
}
}
VS Code Java extension helps generate test method skeletons. Right-click in your Java file and click on Source Action...
, then choose Generate Tests...
.
Using VS Code:
Run with Coverage
.After you are able to run JUnit tests successfully using a dummy test class such as the above, you can add more tests and test classes as necessary.
To learn how to write useful JUnit test cases, refer this section of our SE book. For a quick overview of more advance JUnit features, refer this section.
Contributors: Song Yuexi (@YosieSYX)