Wednesday, August 13, 2008

Java Unit testing

This is a simple test class i wrote to test following method.

package com.fastsearch.admomentum.common.time;

import java.io.Serializable;
import java.util.Calendar;
........

public class DateRange implements Serializable {

.....

public boolean isBetween(Calendar date) {
if (getEnd() != null) {
return (date.after(getStart()) && date.before(getEnd()))
|| date.equals(getStart()) || date.equals(getEnd());
} else {
return date.after(getStart()) || date.equals(getStart());
}
}
}

----------------------------------------------

package com.fastsearch.admomentum.common.time;

import junit.framework.TestCase;
import java.util.Calendar;


public class DateRangeUnitTest extends TestCase {

private DateRange dateRange;

public void testIsBetween()
{
assertFalse(isBetweenOutOfBoundTest());
}

private boolean isBetweenOutOfBoundTest()
{
Calendar testDate = Calendar.getInstance();
testDate.clear();
testDate.set(Calendar.YEAR,2007);
testDate.set(Calendar.MONTH,4);
testDate.set(Calendar.DATE,1);

Calendar startDate = Calendar.getInstance();
startDate.clear();
startDate.set(Calendar.YEAR,2007);
startDate.set(Calendar.MONTH,5);
startDate.set(Calendar.DATE,1);

Calendar endDate = Calendar.getInstance();
endDate.clear();
endDate.set(Calendar.YEAR,2007);
endDate.set(Calendar.MONTH,7);
endDate.set(Calendar.DATE,1);

dateRange = new DateRange(startDate, endDate);
return dateRange.isBetween(testDate);
}
}

No comments: