• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unit test for java

 
sankar balaji
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i write unit test cases for this one?

public class SubscriberLookupResultDto implements Serializable
{
/**
* Represents the number to start at for returned results.
*/
private int startIndex;

/**
* Represents the number of records to be returned in this result set.
*/
private int recordCount;

/**
* Represents the customer details Dto object list.
*/
private CustomerDetailsDto[] subscriberSummaryList;

/**
* Default constructor.
*/
public SubscriberLookupResultDto()
{
}

/**
* Constructor with all the parameters for the Dto
* @param startIndex the number to start at for returned results.
* @param recordCount the number of records to be returned
* in this result set.
* @param subscriberSummaryList the customer details Dto object list.
*/
public SubscriberLookupResultDto (int startIndex,int recordCount,
CustomerDetailsDto[] subscriberSummaryList)
{
this.startIndex = startIndex;
this.recordCount = recordCount;
this.subscriberSummaryList = subscriberSummaryList;
}

/**
* Copy constructor for the Dto
* @param subscriberLookupResultDto the SubscriberLookupResultDto
* object to be copied
*/
public SubscriberLookupResultDto(SubscriberLookupResultDto subscriberLookupResultDto)
{
this.startIndex = subscriberLookupResultDto.getStartIndex();
this.recordCount = subscriberLookupResultDto.getRecordCount();
this.subscriberSummaryList =
subscriberLookupResultDto.getSubscriberSummaryList();
}

/**
* Equals method test equality two Objects
* @param obj1 the Object to be compared.
* @param obj2 the Object to be compared with.
*
* @return boolean value representing the result of the comparison
*/
private boolean isEqual(Object obj1, Object obj2)
{
if (obj1 == null && obj2 == null)
{
return true;
}
if (obj1 == null || obj2 == null)
{
return false;
}
return(obj1.equals(obj2));
}

/**
* Equals method test equality two Object arrys
* @param obj1[] the object array to be compared.
* @param obj2[] the object array to be compared with.
*
* @return boolean value representing the result of the comparison
*/
private boolean isEqual(Object[] obj1, Object[] obj2)
{
if( obj1.length == obj2.length )
{
for( int i=0; i<obj1.length; i++ )
{
if ( obj1[i] == null && obj2[i] == null )
{
continue;
}
if ( obj1[i] == null || obj2[i] == null )
{
return false;
}
if ( !obj1[i].equals(obj2[i]) )
{
return false;
}
}
return true;
}
return false;
}

/**
* Equals method test equality of the object
* @param obj the object to be compared.
*
* @return boolean value representing the result of the comparison
*/
public boolean equals(Object obj)
{
if (obj != null && (obj.getClass().equals(this.getClass())))
{
SubscriberLookupResultDto subscriberLookupResultDto = (SubscriberLookupResultDto )obj;
return ((this.startIndex == subscriberLookupResultDto.getStartIndex()) &&
(this.recordCount == subscriberLookupResultDto.getRecordCount()) &&
isEqual(this.subscriberSummaryList, subscriberLookupResultDto.getSubscriberSummaryList()));
}
return false;
}

/**
* Returns startIndex the number to start at for returned results.
* @return startIndex the number to start at for returned results.
*/
public int getStartIndex()
{
return this.startIndex;
}

/**
* Sets startIndex the number to start at for returned results.
* @param startIndex the number to start at for returned results.
*/
public void setStartIndex(int startIndex)
{
this.startIndex = startIndex;
}

/**
* Returns recordCount the number of records to be
* returned in this result set.
* @return recordCount the number of records to be returned
* in this result set.
*/
public int getResultCount()
{
return subscriberSummaryList.length;
}

/**
* Returns subscriberSummaryList the customer details Dto object list.
* @return subscriberSummaryList the customer details Dto object list.
*/
public CustomerDetailsDto[] getSubscriberSummaryList()
{
return this.subscriberSummaryList;
}

/**
* Sets subscriberSummaryList the customer details Dto object list.
* @param subscriberSummaryList the customer details Dto object list.
*/
public void setSubscriberSummaryList(CustomerDetailsDto[] subscriberSummaryList)
{
this.subscriberSummaryList = subscriberSummaryList;
}

/**
* Returns int the total number of results.
* @return returns the number of records to be returned in this result set.
*/
public int getRecordCount()
{
return this.recordCount;
}

/**
* Sets recordCount the number of records to be returned in this result set.
* @param recordCount the number of records to be returned in this result set.
*/
public void setRecordCount(int recordCount)
{
this.recordCount = recordCount;
}
} sankarb@cybage.com
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34860
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankar,
It looks like the most significant method is the equals method, so that's a good place to start. Try to see which cases look good to test based on your logic. For example, pass null, a different class' object, a different start index, ... and verify that the method returns what you are expecting.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And once the equals method is tested, you can also test the copy constructor (create a copy and assert that it equals the original object) and serialization (serialize to a byte array, deserialize and assert that the deserialized object equals the original).

BTW,



can be refactored to



(assuming that obj1.equals is implemented correctly).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic