Professional Documents
Culture Documents
Junit Tutorial - Testing Framework For Java: Types of Unit Testing
Junit Tutorial - Testing Framework For Java: Types of Unit Testing
J-unit testing
To perform unit testing, we need to create test cases. The unit test case is a code
which ensures that the program logic works as expected.
The org.junit package contains many interfaces and classes for junit testing such as
Assert, Test, Before, After etc.
1) Manual Testing
If you execute the test cases manually without any tool support, it is known as manual
testing. It is time consuming and less reliable.
2) Automated Testing
If you execute the test cases by tool support, it is known as automated testing. It is fast
and more reliable.
The Junit 4.x framework is annotation based, so let's see the annotations that can be
used while writing the test cases.
@AfterClass annotation specifies that method will be invoked only once, after finishing
all the tests.
Assert class
The org.junit.Assert class provides methods to assert the program logic.
Let's write the logic to find the maximum number for an array.
1. package com.javatpoint.logic;
2. public class Calculation {
3.
4. public static int findMax(int arr[]){
5. int max=0;
6. for(int i=1;i<arr.length;i++){
7. if(max<arr[i])
8. max=arr[i];
9. }
10. return max;
11. }
12. }
Here, we are using JUnit 4, so there is no need to inherit TestCase class. The main
testing code is written in the testFindMax() method. But we can also perform some task
before and after each test, as you can see in the given program.
1. package com.javatpoint.testcase;
2.
3. import static org.junit.Assert.*;
4. import com.javatpoint.logic.*;
5. import org.junit.Test;
6.
7. public class TestLogic {
8.
9. @Test
10. public void testFindMax(){
11. assertEquals(4,Calculation.findMax(new int[]{1,3,4,2}));
12. assertEquals(-1,Calculation.findMax(new int[]{-12,-1,-3,-4,-2}));
13. }
14. }
download this example
To run this example, right click on TestLogic class -> Run As -> 1Junit Test.
Output:Assertion Error
As you can see, program logic to find the maximum number for the given array is not
correct because it doesn't return -1 in case of negative values. The correct program logic
is given below:
1. package com.javatpoint.logic;
2. public class Calculation {
3.
4. public static int findMax(int arr[]){
5. int max=arr[0];//arr[0] instead of 0
6. for(int i=1;i<arr.length;i++){
7. if(max<arr[i])
8. max=arr[i];
9. }
10. return max;
11. }
12. }
If you run the junit program again, you will see the following output.
Another example of Junit framework
There are various types of assertions like Boolean, Null, Identical etc.
1. assertTrue(condition)
2. assertFalse(condition)
Null object
If you want to check the initial value of an object/variable, you have the
following methods:
1. assertNull(object)
2. assertNotNull(object)
Identical
If you want to check whether the objects are identical (i.e. comparing two
references to the same java object), or different.
Assert Equals
If you want to test equality of two objects, you have the following methods
assertEquals(expected, actual)
assertArrayEquals(expected, actual)
Above method must be used if arrays have the same length, for each valid
value for i, you can check it as given below:
assertEquals(expected[i],actual[i])
assertArrayEquals(expected[i],actual[i])
Fail Message
If you want to throw any assertion error, you have fail() that always results
in a fail verdict.
Fail(message);
JUnit assertEquals
You have assertEquals(a,b) which relies on the equals() method of the
Object class.
String obj1="Junit";
String obj2="Junit";
assertEquals(obj1,obj2);
For example:
You will create few variables and important assert statements in JUnit.
In this example, you will execute our test class using TestRunner.java
Junit4AssertionTest.java
package guru99.junit;
@Test
public void testAssert(){
//Variable declaration
String string1="Junit";
String string2="Junit";
String string3="test";
String string4="test";
String string5=null;
int variable1=1;
int variable2=2;
int[] airethematicArrary1 = { 1, 2, 3 };
int[] airethematicArrary2 = { 1, 2, 3 };
//Assert statements
assertEquals(string1,string2);
assertSame(string3, string4);
assertNotSame(string1, string3);
assertNotNull(string1);
assertNull(string5);
assertTrue(variable1<variable2);
assertArrayEquals(airethematicArrary1, airethematicArrary2);
}
}
Step 2) You need to create a test runner class to execute above class:
TestRunner.java
package guru99.junit;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
System.out.println(failure.toString());
}
System.out.println("Result=="+result.wasSuccessful());
}
}
1. assertEquals(string1,string2);
Now compare string1=" Junit" with string2=" Junit" with equals method of
object class. Replacing assertEquals method from
java.lang.Object.equals() method :
2. assertSame(string3, string4);
3. assertNotSame(string1, string3);
Since string1="Junit" and string3="test" means both string1 and string3 are
of different types, so assertNotSame(string1, string3) will return true.
4. assertNotNull(string1);
5. assertNull(string5);
6. assertTrue(variable1<variable2);
7. assertArrayEquals(airethematicArrary1, airethematicArrary2);
Summary: