aspose file tools*
The moose likes Testing and the fly likes unit test for java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "unit test for java" Watch "unit test for java" New topic
Author

unit test for java

sankar balaji
Greenhorn

Joined: Jun 24, 2004
Posts: 2
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

Joined: May 26, 2003
Posts: 30764
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unit test for java