Junit is a unit test framework that is used to test a method or piece of code that is only called a unit. In this blog, we will see an end-to-end use of the JUnit framework with real-time examples.

What is Unit Testing

When we write business logic into a unit/method then we try to test the unit on its own. The unit can be an individual method or a group of methods. The advantage of the method once I write the test condition of the particular method, I can run and test the method within milliseconds and check whether all the test conditions are passing or not.

Example: In the below example I want to test a method that will return the age of a user after 10 years. So let’s suppose I have given input as the current age is 25. so the expected output should be 35.

The method that needs to be a test

public long ageAfer10Year(long currentAge) {
  return currentAge+10;
 }

Junit test to test the above method

@Test
 public void testAgeAfter10Years() {
  long age =	user.ageAfer10Year(25);
  assertEquals(35, age);
  //assertEquals(long expected, long actual)
 }

@Before annotation in Junit

@Before annotation will use to run a method before each testcase into a class. It means If I want to perform some operation before each test then I can write that method with @before annotation.

@Before
public void setUp() {
 System.out.println("Before Each Test.");
 user = new User();
}

@After annotation in Junit

@After annotation will use to run a method after each testcase into a class. It means If I want to perform some operation after each test then I can write that method with @after annotation.

This is basically used for teardown operations like closing database connections.

@After
 public void tearDown() {
  System.out.println("After Each Test.");

 }

@BeforeClass annotation in Junit

@BeforeClass annotation allows running a method only once before the class. It allows only static methods so we can not use @Beforeclass with nonstatic methods.

@BeforeClass
 public static void beforeClass() {
  System.out.println("Before Class.");
 }

@AfterClass annotation in Junit

@AfterClass annotation allows running a method only once after the class. It allows only static methods so we can not use @afterclass with nonstatic methods.

@AfterClass
 public static void afterClass() {
  System.out.println("After Class.");
 }

How to compare arrays into JUnit

To compare arrays or ArrayList in Junit we use “assertArrayEquals(expected, actual)“. assert method will take two inputs expected array and an actual array.

Below is an example to compare or validate an array in Junit.

@Test
public void testArray() {
 
 long[] ar = {8,2, 6,};
 Arrays.sort(ar);
 long actualarray [] = {2,6,8};
   assertArrayEquals(ar, actualarray);
//assertArrayEquals(expected, actual);
 
}

How to test Performance in JUnit

timeout attribute is used to set an execution time for a testcase. For example in the below code snippet, I going to run a loop 1000000 times and set the time as 10ms.

syntax: @Test(timeout=10)

The default time unit is MS.

@Test(timeout=10)
public void testPerformance() {
 
 long[] ar = {8,2, 6,};
 for(int i =0; i<1000000; i++) {
  Arrays.sort(ar);
  long actualarray [] = {2,6,8};
  assertArrayEquals(ar, actualarray);
 }

}

Create TestSuite in Junit

Create a Junit testsuite class and add the attribute @RunWith(Suite.class) and @SuiteClasses({ UserTest.class, UserTest2.class }), The @SuiteClasses annotation contains a collection of classes that you want to run in this specific test suite.

package com.junitlearn.user;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ UserTest.class, UserTest2.class })
public class AllTests {

}

Important Checks that you should remember about Junit

  • TestMethod should be void and public only.
  • @BeforeClass and @AfterClass methods should be static.
  • The test method name should start with “test”