JUnit is a testing framework 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 explains how to use JUnit in a project, with Gradle.
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
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
}
}
If using an IDE, restart the IDE after updating the build.gradle
file.
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);
}
}
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.
In Intellij IDEA:
src/test/java/seedu/DukeTest.java
), 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:
src/test/java
folder), right-click on the folder, and choose Run Tests in '...'
.Using Gradle:
test
(more info on running Gradle tasks)test
task] The location of the test
task in the Gradle task hierarchy is Tasks -> verification -> test
(see screenshot below).Other ways:
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.