Saturday, May 13, 2017

Unit Testing in Java

Why Testing? 

Testing is necessary to deliver a good quality product. Nobody is perfect and the development phase of a software is prone to error. So, if we find the software bugs at the beginning phase, we can reduce the maintenance cost. So, we need proper testing while we do programming and the developer can himself carry out some tests whether his function is working as desired. Basically, it is tedious to manually test all possibilities and the developer himself has tendency to test best case scenarios. But the output product is used by users who never know the details about it. So, a third party user has higher possibilities to discover bugs. But we do not get this chance until we deliver the product, and the customer also do not like buggy software, so the final delivered product should be Bug-Free.

JUnit Testing

In this article, I am sharing the steps on how we can use JUnit framework to test java classes.  To use JUnit testing, we need to include library file in class path. It can be downloaded from www.junit.org and include it as reference library.

How?

1) The unit testing files should be normally separated, you know why? These files are testing files only needed to developer, not the user. So, it is good practice not to include these test files in the production mode. Thus we create a separate folder to store test files.

2) Packaging makes it everything clear. So, we create the same package, and test classes with suffix Test.

Suppose, we create a class

package com.krishna.math;

public class MathFunction{
       private  int A;
       private int B;
       public MathFunction(int A, int B){
         this.A=A;
         this.B=B;
     }
    
public int getSum(){
     return this.A+this.B;
}

public int getDiff(){
    return this.A-this.B;
}  




Now, we create a test class. The above mentioned class MathFunction resides in src folder, while we create another folder test to store test files. Gradle and maven projects default structure is:

source: /src/main/java
test: /src/test/java

So, under test folder, we define a class to test the above-mentioned class functions:

package com.krishna.math;

import static org.junit.Assert.*;

import org.junit.Test;

public class MathFunctionTest{
     //Write test methods here
     @Test 
     public void testSum(){
       MathFunction mathFunction=new MathFunction(12,34);
       assertEquals(46, mathFunction.getSum());
     }

   @Test
   public void testDIff(){
    MathFunction mathFunction=new MathFunction(13,12);
    assertEquals(1, mathFunction.getDiff());
    }
}


So far so good, we can run it directly from eclipse, just right click and run as JUnit. That shows the test result. 

Running Multiple Tests (TestSuite Class )

The above mentioned method runs one test case (test class) at at time. But we are interested to test all test cases as a whole. So, the need of test suite class arises.

We define a test suite class as follows:

import org.junit.runner.RunWith
import org.junit.runners.Suite;

import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ LinkedListTest.class, MyStackTest.class })
public class AllTests {

}

After, we run it from eclipse IDE, it works like a charm. You see the result with proper coloring also, Eclipse helps you!

Test Automation 
So far, we have created test cases and test suite which is run by eclipse. What if you wanna run and get result and do some process. For that you need a test runner. It is an independent runner and you have control over the result of the test. You may create a separate class for test runner or just add it in TestSuite class. 


I create a separate class as follows:

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner{
public static void main(String[] args) {
Result result = JUnitCore.runClasses(AllTests.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}

System.out.println(result.wasSuccessful());

}
}


The examples presented are just sample, the real test cases tend to have many methods. 





No comments:

Post a Comment