Tuesday, January 25, 2011

Junit Tutorial 1 : Junit with an example


Junit is very useful for  unit testing java code.
Let us understand with a simple example that will illustrate the basic concepts involved in testing with JUnit.


The Goal :

Creating a java file named Calculator.java which has a method named sum() which takes two int parameters
and return addition of these two numbers.
So here we have to check whether this method is functioning well in all the conditions or not.

Creating Calculator.java :
This class has sum method which takes two int parameters to add them and return it . Save and compile this file.

public class Calculator{
    int sum(int num1,int num2){
        return num1+num2;
    }
}
Creating CalculatorTest.java :
To test that method sum() is working fine we need to check it.
For this we create another class named CalculatorTest. Save and compile this file.
Before proceeding further we should first have a look over JUnit coding convention.

        Coding Convention :
                1. Name of the test class must end with "Test".
                2. Name of the method must begin with "test".
                3. Return type of a test method must be void.
                4. Test method must not throw any exception.
                5. Test method must not have any parameter.

In our example, the class name which we are going to test is "Calculator" so we have created class "CalculatorTest" here.
Let us write the test case.



In the same way the method created to test the particular method will be appended with test word as in this example we are going to test the method sum() hence we have created method testSum() in this class.

import junit.framework.TestCase;


public class CalculatorTest extends TestCase {
    Calculator cal=new Calculator();
    
     public CalculatorTest(String name) {
             super(name);
     }
     
     public void testSum() {
             assertEquals(2,cal.sum(1,1));
    }
}

Explanation :

import junit.framework.TestCase;

As we need to use some classes of JUnit constructs in the testing program so have to use import statement to use them.
In this example, we are going to use TestCase class so we need to import this class from framework package of JUnit.

public class CalculatorTest extends TestCase

If we want to define our own test methods then we have to extend TestCase class in our testing class.
In this example we are going to test the functionality of adding two numbers.
So we have created our class named CalculatorTest.
In this class there is one method to testSum to test addition functionality of the program.
To make these methods of any use our class extends TestCase class.

public CalculatorTest(String name) {
             super(name);
     }
When we test the functionality then we can see the output to check which test has produced error i.e. which test fails.
So for this every test is given name. The constructor of the class provides this functionality
by passing this parameter to the constructor of the parent class.

public void testSum() {
             assertEquals(2,cal.sum(1,1));
    }
The method testSum() is to test the functionality of addition in the Calculator.java class.
In this example, there is a need for only one method but we can add as many methods as we need.
We can also define variables and perform arithmetic calculations just as we do in any Java program.
In this method we are checking whether the value returned by sum method of the object of the Calculator class is equal to "2".
If it is so then the test will be shown successful.
If the method did not perform as expected then it will cause assertEquals() to fail.
Now we need to fix the problem and run the test again . We need to repeat this process until the test is passed.

How to run JUnit in text mode :

Execute java junit.textui.TestRunner CalculatorTest.

No comments:

Post a Comment

subversion video