JUnit — unit testing framework

Jay Patel
3 min readMay 6, 2021

--

What Is a Unit-Testing?

  • Unit-Testing is a level of software testing where individual units/components of the software are tested.
  • The purpose of unit testing is to validate that each unit of the software performs as designed.

Unit Testing = Actual working of system Vs. Expected working of the system

Introduction to JUnit

JUnit is an open-source unit testing framework that is used to write and run repeatable automated tests, so that we can ensure that our code works as expected. JUnit plays a crucial role in test-driven development and it follows the idea of “first testing than coding”.

Important features of JUnit

  • It is very simple to use.
  • You can develop more readable, reliable, and bug-free code.
  • Provides assertions, annotations, and test methods.
  • It improves code quality.
  • Tests can be run automatically.

JUnit annotations

Example to demonstrate the use of annotation:

package …

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.util.ArrayList;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class JunitAnnotationsClass {

private ArrayList<String> list;

@BeforeClass
public static void beforeClass() {
System.out.println(“@BeforeClass”);
}

@Before
public void before() {
list = new ArrayList<String>();
System.out.println(“@Before”);
}

@After
public void after() {
list.clear();
System.out.println(“@After”);
}

@AfterClass
public static void afterClass() {
System.out.println(“@AfterClass”);
}

@Test
public void Test() {
list.add(“test”);
assertEquals(1, list.size());
}

@Ignore
public void ignore() {
System.out.println(“@Ignore”);
}
}

output:

@BeforeClass

@Before

@After

@AfterClass

JUnit Assertions

Example to demonstrate the use of assertions:

package …

import static org.junit.Assert.*;
import org.junit.Test;

public class AssertionsTest {

@Test
public void test() {

String obj1 = “junit”;
String obj2 = “junit”;
String obj3 = “test”;
String obj4 = “test”;
String obj5 = null;

int var1 = 1;
int var2 = 2;

assertEquals(obj1, obj2);
assertTrue(var1 == var2);
assertFalse(var1 == var2);
assertNotNull(obj1);
assertNull(obj5);
}
}

  • The assertEquals() method will run normally if the two compared objects are equal, otherwise, a failure will be displayed in the JUnit window and the test will abort.
  • The assertTrue() and assertFalse() methods tests if a condition or a variable is true or false.
  • The assertNull() and assertNotNull() methods test whether a variable is null or not null.

Demonstration of creating and running test cases:

Example: Program to find the maximum element of the array

package …

import java.io.*;

public class Testable {
public static int findMaximum(int arr[]) {
int max = arr[0];
for(int i = 1; i < arr.length; i++) {
if(max < arr[i])
max = arr[i];
}
return max;
}
}

In the above source code, we can notice that the class `Testable` has one public method named findMaximum(), which gets an array as input and returns the maximum element of the array. To test this method, we have to create another class that will test each one of the methods of the `Testable` class (in this case, we have only one method to be tested).

Unit Tests for the Testable.java:

package …
import static org.junit.Assert.*;
import org.junit.Test;

public class TestRunner {

Testable testable= new Testable();

@Test
public void testFindMaximum(){
int[] arr1 = {1,3,4,2};
int[] arr2 = {-12,-1,-3,-4,-2};

assertEquals(4, testable.findMaximum(arr1));
assertEquals(-1, testable.findMaximum(arr2));
}
}

Firstly, we can see that there is a @Test annotation above the testFindMaximum() method. This annotation indicates that the public void method to which it is attached can be run as a test case. We can also observe a method called assertEquals(4, testable.findMaximum(arr1)). This method assertEquals (object expected, object actual) takes as inputs two objects and asserts that the two objects are equal.

--

--