Thursday, June 27, 2013

Simple Example for using JUnit with Eclipse to test Java source code

This is a very basic tutorial on how to begin with JUnit to perform unit testing of the java source code you have written.

Why should we perform unit testing?

No software in the world can be said to be completely bug free. But it is important that you test what ever piece of software you write and make sure of removing almost all possible bugs. For this we can follow two important principles which most of the experienced programmers follow.
  1. Make your functions simple. Follow the unix design principle. Each function should do only one simple task. Do not complicate your function and let it handle all the possible cases. Follow the principles of ADT. It becomes very easy to test your functions if you follow this principle.  
  2. Think of how to test when you write each function. List down the test cases your function should pass and   most importantly include the test cases for which your function should fail. The function should behave as expected in all possible cases.

So what is JUnit?

JUnit is a framework which can be used to perform unit testing of your java code.

How to use JUnit to Unit Test java code?

Let me explain you this with a very small example. We will have class Calculator which can perform all the 4 basic operations like Addition, Subtraction, Multiplication and Division. Now lets learn how to test these operations using JUnit.

package learn.junit.demo;

public class Calculator {
 
 public double add(double a, double b) {
  return a + b;
 }
 
 public double subtract(double a, double b) {
  return a - b;
 }
 
 public double multiply(double a, double b) {
  return a * b;
 }
 
 public double divide(double a, double b) { 
  if(b == 0) {
   throw new ArithmeticException();
  }
  return a / b;
 }
 
}
Follow the step by step instruction to successfully create and execute the test cases.

Step 1: Create a java Project names Calculator and create a class Calculator as shown above. Now right click on the Calculator class in the eclipse project explorer and select JUnit Test case. If you don't this option, then select New --> Other... --> Java --> Junit --> JUnit Test Case.



Step 2: Enter the name of the test case file, package where it has to be create and also select the setUp() and tearDown() methods if required and click on next



Step 3: Select the methods you want to test and click on finish



Step 4: Now you get a list of test cases, one each for the methods you have implemented in the Calculator class. Now you can write your test cases and execute them.


Step 5: Just copy paste the implementation given below

package learn.junit.demo;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {

 private Calculator calc;
 
 @Before
 public void setUp() throws Exception {
  calc = new Calculator();
 }

 @After
 public void tearDown() throws Exception {
  calc = null;
 }

 @Test
 public void testAdd() {
  double result = calc.add(10.5, 20.32);
  assertEquals(30.82, result, 0);
 }

 @Test
 public void testSubtract() {
  double result = calc.subtract(100.5, 100);
  assertEquals(0.5, result, 0);
 }

 @Test
 public void testMultiply() {
  double result = calc.multiply(2.5, 100);
  assertEquals(250, result, 0);
 }

 @Test
 public void testDivide() {
  double result = calc.divide(100, 10);
  assertEquals(10, result, 0.001);
  
 }
 
 @Test(expected = ArithmeticException.class)
 public void testDivideByZero() {
  calc.divide(100.5, 0);
 }

}

Step 6: Now execute the test cases as shown below by right clicking on the CalculatorTest.java and selecting Run As --> JUnit Test


For understanding the JUnit APIs you can refer to the API documentation.

The figure below gives a very brief explanation about the APIs used for implementing the test cases.



Next Blog will be on using EasyMock to mock the interfaces during unit testing.