Guides for SE student projects »

Using JUnit

JUnit is a testing framework for Java.

Sections below explains how to use JUnit in a project.

Conventions to follow

  1. Add test code in a folder named [project root]\src\test\java\ folder.
  2. Name the test class to match the class being tested ( can be tested by, 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).
    For example,
    • Class being tested seedu.duke.Todo: src\main\java\seedu\duke\
    • Test class seedu.duke.TodoTest: src\test\java\seedu\duke\
[project root] e.g., C:\courses\project\ └── src\ ├── main\ │ └── java\ │ └── seedu\ │ └── duke\ │ └── └── test\ └── java\ └── seedu\ └── duke\ └──

Adding JUnit support to your project

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 recommend using Gradle as it can make things easier in the long run.

Prerequisite: The project is configured to use Gradle already. If you have not done that yet, follow the Gradle Tutorial to add Gradle support to the project first.

1. 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, add the following two 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'

Next, add the following, to tell Gradle that JUnit is to be used as the testing tool.

test {

If using an IDE, restart the IDE after updating the build.gradle file.

2. 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\, you can add a test class src\test\java\seedu\duke\ 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 {
    public void dummyTest(){
        assertEquals(2, 2);

    public void anotherDummyTest(){
        assertEquals(4, 4);

3. Run tests, either using the Intellij UI (preferred -- this makes debugging failed test cases easier) or using Gradle itself, as explained in the section below.

Running tests

In Intellij IDEA:

  • To run a specific JUnit test class (e.g., src/test/java/seedu/, right-click on the test class, and choose Run {classname}.

If the above doesn't work, you may want to go to File > Settings and change theRun tests using: setting to Intellij IDEA (instead of Gradle), as shown below:

Expand to see screenshot ...

change Intellij settings to not use Gradle

  • To run all tests in a folder (e.g., src/test/java folder), right-click on the folder, and choose Run Tests in '...'.
  • Other supported IDEs (e.g., Eclipse, NetBeans, VS Code, etc.) have similar mechanisms.

Using Gradle::

  • To run all tests in the project, run the Gradle task test (more info on running Gradle tasks)
  • [If using Intellij UI to run the test task] The location of the test task in the Gradle task hierarchy is Tasks -> verification -> test (see screenshot below).

Other ways:

Writing useful JUnit tests

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.