JUnit is an open-source testing framework for java programmers. The programmer can create test cases and test their code. It is a unit-testing framework.
To perform unit testing, you need to create test cases. The unit test case is a code used to ensure that the program logic works as expected.
The org.junit package comprises many interfaces and classes for JUnit testing, such as Assert, Test, Before, After, etc.
Types of unit testing:
Unit testing can be performed in two ways:
- Manual Testing: If test cases are executed manually without any tool support, it is known as manual testing. It is time-consuming and less reliable.
- Automated Testing: If test cases are executed by tool support, it is known as automated testing. It is fast and more reliable.
Annotations for JUnit testing:
The JUnit framework is annotation based that can be used while writing the test cases.
- @Test annotation specifies that the method is the test method.
- @Test(timeout=1000) annotation specifies that the method will be failed if it takes longer than 1000 milliseconds (1 second).
- @BeforeClass annotation specifies that the method will be invoked only once, before starting all the tests.
- @Before annotation specifies that the method will be invoked before each test.
- @After annotation specifies that the method will be invoked after each test.
- @AfterClass annotation specifies that the method will be invoked only once, after finishing all the tests.
Assert class:
The org.junit.Assert class contains methods to assert the program logic. The common methods of Assert class are:
- void assertEquals(boolean expected, boolean actual): it checks that the two primitives/objects are equal.
- void assertTrue(boolean condition): checks that the condition is true.
- void assertFalse(boolean condition): checks that the condition is false.
- void assertNull(Object obj): checks that the object is null.
- void assertNotNull(Object obj): checks that the object is not null.
Example:
Step 1: Write a simple program to find the maximum number in an array.
package com.example.logic; public class Calculation { public static int findMax(int arr[]){ int max=0; for(int j=1;j<arr.length;j++){ if(max<arr[j]) max=arr[j]; } return max; } }
Step 2: Write the test case
Here, we are using JUnit, so there is no need to inherit the TestCase class. The main testing code will be written in the testFindMax() method. However, we can also perform some tasks before and after each test, as shown in the given program.
package com.example.testcase; import static org.junit.Assert.*; import com.example.logic.*; import org.junit.Test; public class TestLogic { @Test public void testFindMax(){ assertEquals(4,Calculation.findMax(new int[]{1,3,4,2})); assertEquals(-1,Calculation.findMax(new int[]{-12,-1,-3,-4,-2})); } }
To run the above example, right-click on TestLogic class > Run As > 1Junit Test.
Output: Assertion Error
The above output is displayed in Eclipse IDE.
Here you can see that when we pass the negative values, it throws AssertionError because the second-time findMax() method returns 0 instead of -1. It means our program logic is incorrect.
Correct program logic:
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:
package com.example.logic; public class Calculation { public static int findMax(int arr[]){ int max=arr[0]; for(int j=1;j<arr.length;j++){ if(max<arr[j]) max=arr[j]; } return max; } }
If you rerun the JUnit program, you will see the following output.
Another example of the JUnit framework
Step 1: Write the program code
package com.example.logic; public class Calculation { public static int findMax(int arr[]){ int max=0; for(int j=1;j<arr.length;j++){ if(max<arr[j]) max=arr[j]; } return max; } public static int cube(int n){ return n*n*n; } public static String reverseWord(String str){ StringBuilder result=new StringBuilder(); StringTokenizer tokenizer=new StringTokenizer(str," "); while(tokenizer.hasMoreTokens()){ StringBuilder sb=new StringBuilder(); sb.append(tokenizer.nextToken()); sb.reverse(); result.append(sb); result.append(" "); } return result.toString(); } }
Step 2: Write the test case
package com.example.testcase; import static org.junit.Assert.assertEquals; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import com.example.logic.Calculation; public class TestCase2 { @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("before class"); } @Before public void setUp() throws Exception { System.out.println("before"); } @Test public void testFindMax(){ System.out.println("test case find max"); assertEquals(4,Calculation.findMax(new int[]{1,3,4,2})); assertEquals(-2,Calculation.findMax(new int[]{-12,-3,-4,-2})); } @Test public void testCube(){ System.out.println("test case cube"); assertEquals(27,Calculation.cube(3)); } @Test public void testReverseWord(){ System.out.println("test case reverse word"); assertEquals("ym eman si nahk",Calculation.reverseWord("my name is khan"); } @After public void tearDown() throws Exception { System.out.println("after"); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("after class"); } }
Output:
before class before test case find max after before test case cube after before test case reverse word after after class
2 Responses