Introduction
Junit is an open-source testing framework for programmers working with java. It is one of the primary unit testing frameworks, and the current version is junit 4.
The core Junit Api contains multiple packages and interfaces that you can import in a test suite according to the user requirements. All these packages are essential for performing Unit Testing using Junit.
Suppose you are unfamiliar with the concept of Unit Testing. In that case, you can check out our article on writing your very first unit test using the Jest testing framework (a testing framework in Javascript) here.
The org.junit Package
The org.junit is the core Junit package corresponding to the Junit framework. This package provides all the core classes and annotations of Junit.
The Classes of this package are as follows:
Class
-
Assert: Provides a set of assertion methods that you can use for writing tests. That includes methods like assertEquals, assertFalse, assetTrue all of these take boolean as a parameter and check against condition to be Equals, True, or False, respectively.
Other methods like assertNull, assertNotNull take objects as parameters to check the Null condition. And fail(), which fails a test with no message. - Assume: Provides a set of methods used for stating assumptions about conditions in which a test is meaningful.
- Test.None: This is the default empty exception.
The Junit framework is annotations-based; therefore, the Junit package also includes some annotations. The org.junit package consists of the following annotations:
Annotations
- Before: We use this in tests requiring you to create similar objects before running a test.
- After: You can use this when you need to release external resources after running a test if you allocate external resources in a Before.
- BeforeClass: You can use this when tests need to share a computationally expensive setup (like logging into a database).
- AfterClass: You can use this when you need to release expensive external resources after all the tests in the class have run. Suppose you allocate them in a BeforeClass method.
- Ignore: Sometimes, you will want to temporarily disable a test or a group of tests to use this method.
- Test: This annotation tells JUnit that the public void method to which the Test is attached can run as a test case.
Example
We will try out some of these methods with an elementary example. Firstly let us create a java class file with the test code (example testjunit1.java) in your project directory.
Program
import org.junit.Test;
import static org.junit.Assert.*;
public class testjunit1 {
@Test
public void testAdd() {
//test data
int num = 5;
String temp = null;
String str = "Junit is working fine";
//check for equality
assertEquals("Junit is working fine", str);
//check for false condition
assertFalse(num > 6);
//check for not null value
assertNotNull(temp);
}
}You can see that we have imported org.junit package at the top level. We have also used Assert class and Test annotation in this class file. Now that we have our class file, we can create a class file (example testrunner1.java) in the same directory.
Program
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class testrunner1 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(junittest1.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
} You can verify the output by firstly running the following command to run the Test Runner classes on the terminal.
WorkingDirectoryPath> javac testjunit1.java testrunner1.java
You can now run the Test Runner that runs the test case.
C:\JUNIT_WORKSPACE>java TestRunner1
Output
true




