A Brief Introduction About JUnit

Posted By : Kiran Sharma | 28-Jun-2018

 

 

 

Testing is a process to check whether the developed application is working as per the requirements or not. Unit testing is a testing at developer's level. Unit testing is a testing of single module(class or method). We can perform unit testing manually or automated. Automated testing is faster than manual testing and takes less number of testers. Automated testing is more reliable than manual testing.

 

Unit Test Case => Unit test case is a part of code which tests other part of code and ensures whether it works as expected. There must be atleast two test cases for each requirement ,one positive and one negative.

 

Junit is an open source framework for unit testing. It is very useful for java developers to test their code. To follow test driven methodology we must write and execute unit test before any code. Junit says “first testing then coding”, it increases the productivity and stability of the program code. It also reduces the time spent on debugging.

 

Test Coverage : The amount of code which is tested by unit test is termed as test coverage.

 

Junit testing : A JUnit test is a method written in a class which is used only for testing purpose and the class which keeps this method is called test class. To term a method as a test method , we need to use @Test annotation. Under this test method we can use assert statements to check expected result and actual result.

 

 

 

 

 

Demo = >

 

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;

public class Util {
	static Integer add(Integer a, Integer b) {
		return a + b;
	}

	public static void main(String[] args) throws IOException {
		Integer x = 0, y = 0;

		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		System.out.println("Enter x ");
		try {
			String a = br.readLine();
			x = Integer.parseInt(a);
		} catch (NumberFormatException e1) {

		}
		System.out.println("Enter y ");
		try {
			String b = br.readLine();
			if (b != null) {
				y = Integer.parseInt(b);
			}
		} catch (NumberFormatException e2) {

		}
		System.out.println(add(x, y));
	}
}

        

 

Output

Demo =>

public class Util {
	static Integer add(Integer a, Integer b) {
		return a + b;
	}

	public static void main(String[] args) {
		System.out.println("Addition is " + add(5, 6));
		System.out.println("Addition is " + add(5, null));
	}
}
        

Output 

In the above code, if a null value is entered , then NullPointerException is coming and which is not handled here. To test such kind of code developer can take help of Junit.

For this implementation ,first of all we need to add Junit jar file in our project. Here we have added

junit-4.12.jar file in our project. Now we are creating one class named as UtilTest which have one test method represented with @Test annotation.  The test method testNumbers() will test the

add(Integer,Integer) method of Util class.

 

Demo =>

import org.junit.Test;

import junit.framework.TestCase;

import static org.junit.Assert.*;

public class UtilTest extends TestCase {
	@Test
	public void testNumbers() {
		assertNotEquals(null, Util.add(10, 2));
		//assertNotEquals(null, Util.add(10, null));
	}
}
 

 

Execution of this code using STS

Go to Run As - select JUnit Test

If the test case is passed then the output will be like with green progress status.

 

 

And if the test case is failed then the progress bar will be in red.

 

So with the help of this we are able to track the issue before its final implementation.

 

Demo =>


public class Util {
	static Integer add(Integer a, Integer b) {
		if(a==null){
			a=0;
		}
		if(b==null){
			b=0;
		}
		return a + b;
	}
	public static void main(String[] args) {
		System.out.println("Addition is " + add(5, 6));
		System.out.println("Addition is " + add(5, null));
	}
}
        

 

Output

 

And JUnit have also passed this code Output

 

 

 

 

 

 

 

 

 

 

 

About Author

Author Image
Kiran Sharma

Kiran has good knowledge of java with Servlets, JSPs, Spring, and hibernate frameworks. She is very honest towards her work. Her hobby is listening to music.

Request for Proposal

Name is required

Comment is required

Sending message..