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 testi
Unit Test Case => Unit test case is a pa
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 test
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
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 imp
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
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
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.