Using Junit In Eclipse

Unit testing is defined as “ Each module is tested alone in an attempt to discover any errors in its code ”. Its always a good practice to test each unit of your project independently as it saves time and effort while integrating the modules.

JUnit provides close relationship between testing and development. As developers write the code, they use the JUnit test runners to verify how much it deviates from the intended goal. Normally it is a standard practice that the code developers themselves write unit tests for their code.

First step is to put “ junit.jar ” on the build Path. Go to project properties and click “ Java Build Path ”. In the Libraries tab click “ Add External JARs ” and then browse to “ junit.jar ”.

Suppose we want to test method called “ getSmallest() ” in class “ Test.MainClass ”. This method takes an integer array as input argument and returns smallest integer from that array. We will write “ TestCase ” to test this using Junit.

package Test;
 
import java.util.*
public class MainClass {
 
public static void main(String[] args) {
 
 }
 
public static int getSmallest(int arr[]){
 
 	Arrays.sort(arr);
 
 	return arr[0];
 
 }
 
}

Right click “ project name ” and click “ New ” – > “ Junit Test Case ”. New JUnit Test Case window will open. Name the test case and select class for which you are writing the test case. In my case, I selected Test.MainClass. Click Next.

Now we have to select the methods from the chosen class for which test stubs will be generated. In my case, I selected “ getSmallst(int[]) ”. “ Click Finish ”. Test stubs for the method is generated but we have to write a little code to test it.

Review the code below:

package Test;
 
import junit.framework.TestCase;
 
public class test_case_getSmallest extends TestCase {
 
public test_case_getSmallest(String arg0) {
 
 	super(arg0);
 
 }
 
protected void setUp() throws Exception {
 
 	super.setUp();
 
 }
 
protected void tearDown() throws Exception {
 
 	super.tearDown();
 
 }
 
public void testGetSmallest() {
 
MainClass o = new MainClass();
 
 	int[] myarray ={100,200,30};
 
int val;
 
 	if((val = o.getSmallest(myarray)) != 30)
 
 	{
 
 		System.out.println("Shortest value returned is : " + val);
 
 		fail("Not right result.");
 
 	}
 
}
 
}

To run it, right click the test case (test_case_getSmallest.java) and click “ Run As ” – > “ JUnit Test ”. Test will run and the results will be shown. In my example, I got the following results.

Now to see failures. We changed the “ testGetSmallest() ” code as

public void testGetSmallest() {
 
MainClass o = new MainClass();
 
 	int[] myarray ={100,200,30};
 
int val;
 
 	if((val = o.getSmallest(myarray)) != 100)
 
 	{
 
 		System.out.println("Shortest value returned is : " + val);
 
 		fail("Not yet implemented");
 
 	}
 
}

Output I was shown is:

In this example,we are just testing a method using a simple test case. We can test more than one methods using same test case as well. For instance, we could have more than one methods in our class “ MainClass ” and then more than one test stubs in our test case “ test_case_getSmallest() ”.