Wednesday, February 11, 2009

Writing Test Cases with JUnit

JUnit is one of the most famous test case development frameworks for Java programmers.

It is also easy to integrate into Eclipse IDE.

If your projest is Maven enabled, then just by adding JUnit dependencies in your project's pom.xml file, you will be able to start generating test cases for your project. Add this dependency in your project's pom.xml file.
 
   junit
   junit
   3.8.2 
 

Save your pom.xml file and it will start downloading JUnit jar file into your Maven repository.

It is a good practice to place your test cases into the same project with your classes to be tested.

So create a folder named with "src/test/java" into your project and create a new package into it which is going to contain your JUnit test cases.

After adding Junit jar file and creating a folder which is going to contain your test cases, your directory structure is going to look like in Eclipse as follows.

eclipse project with junit dependency


Now add your class to be tested into org.junit.sample package.

Assume that, you are reading a flat file from your local disk containing mobilePhone number, id number and startDate of Employees separated by semicolon ";" .

Your simple Employee.java class contains:
package org.junit.sample;
import java.util.Date;
public class Employee {

 public Employee(){
 }
 String mobilePhone;
 String IdNumber;
 Date startDate;
 public String getMobilePhone() {
  return mobilePhone;
 }
 public void setMobilePhone(String mobilePhone) {
  this.mobilePhone = mobilePhone;
 }
 public String getIdNumber() {
  return IdNumber;
 }
 public void setIdNumber(String idNumber) {
  IdNumber = idNumber;
 }
 public Date getStartDate() {
  return startDate;
 }
 public void setStartDate(Date startDate) {
  this.startDate = startDate;
 } 
}


Sample file (Employee_Data) content is as follows, each line contains 3 significant parts separated by ";" :

3452701678;44522445566;12.09.2008 09:34:10
2133223914;56705003122;12.09.2008 10:36:31

and it is located at your home folder.

Java class that is required to be tested is a class that reads contents of the above text file and parses it to store each element of it's lines in an Employee object.

Simple FileService.java class which is located in the same package with Employee.java is as follows :
package org.junit.sample;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FileService {

 public FileService(){
 }
 
 public Boolean parseTextFile(){
  String readFileFrom = "Employee_Data";
  String strLine,dateTime;
  String[] strArr= new String[3];
  
  File file = null;
  FileInputStream fstream = null;
  DataInputStream dataInputStream = null;
  BufferedReader br = null;
  
  Employee employee = null;
  List<Employee> list = null;
  DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  Date date;
  try {
   file = new File(readFileFrom);   
   fstream = new FileInputStream(file);
   dataInputStream = new DataInputStream(fstream);
   br = new BufferedReader(new InputStreamReader(dataInputStream));
   list = new ArrayList<Employee>();
   while ((strLine = br.readLine()) != null) {
         System.out.println (strLine);
         strArr = strLine.split(";");
         employee = new Employee();
         employee.setMobilePhone(strArr[0]);
         employee.setIdNumber(strArr[1]);
         dateTime = strArr[2];
         date = (Date)formatter.parse(dateTime);
         employee.setStartDate(date);
         list.add(employee);
         employee = null;
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ParseException e) {
   e.printStackTrace();
  }
  if(list != null && list.size() > 0)
   return true;
  else
   return false;
 }
}

Related test case is located in the same Maven enabled project under "/src/test/java" folder. 


FileServiceTest.java class extends from junit framework's TestCase class. testparseTextFile() method in FileServiceTest.java class is used to test FileService.java class's parseTextFile() method.



package org.junit.sample.test;
import org.junit.sample.FileService;
import junit.framework.TestCase;

public class FileServiceTest extends TestCase {
 public FileServiceTest(){
 }
 public void testparseTextFile() throws Exception {
         Boolean readFileResult = false;
  FileService fileService = new FileService();
  readFileResult = fileService.parseTextFile();
  assertTrue(readFileResult);
 }
}



In order to start debugging your FileServiceTest.java class, right click on the name of the test method that you want to run from Outline view of Eclipse and then from the context menu choose :
Debug As -> JUnit Test.


eclipse run junit test case in debug mode



It is not taking long time to prepare Eclipse for JUnit test case development. 

Writing test cases efficiently decreases the amount of time spent for development and testing of your application.

No comments:

Post a Comment