I am new here and new to junit testing. I have a class with two methods and I want to write unit tests for it. I am not sure how to start I read some basic tutorials but I am not able to start some how. Can anyone of you provide me some basic skeleton to start with.
My class is
public class CreateCSV
{
MyWriter csvOutput = null;
public void createSortedSet ( final HashMap< String, Signal > map, final long totalSize, final long totalSizewithHeader, File file )
{
ArrayList< Signal > messages = new ArrayList< Signal >();
try
{
messages.addAll( map.values() );
map.clear();
for ( Signal signal : messages )
{
signal.setBandwidth( ( signal.getSize() / ( float ) totalSize ) * 100 );
signal.setBandwidthWithHeader( ( signal.getSizewithHeader() / ( float ) totalSizewithHeader ) * 100 );
}
Collections.sort( messages, new SignalComparator() );
}
catch ( Exception e )
{
logger.error( "Error in creating messages data", e );
}
createCSV( messages, file );
}
public void createCSV ( final ArrayList< Signal > messages, File file )
{
try
{
// Use FileWriter constructor that specifies open for appending
csvOutput = new MyWriter( new FileWriter( file, false ), ',' );
// Create Header for CSV
csvOutput.writeRecord( "Message Source" );
csvOutput.writeRecord( "Message Name" );
csvOutput.writeRecord( "Component" );
csvOutput.writeRecord( "Occurance" );
csvOutput.writeRecord( "Message Payload with Header" );
csvOutput.writeRecord( "Bandwidth(with Header %)" );
csvOutput.writeRecord( "Message Payload" );
csvOutput.writeRecord( "Bandwidth(%)" );
csvOutput.endOfRecord();
for ( Signal signal : messages )
{
csvOutput.writeRecord( signal.getSource() );
csvOutput.writeRecord( signal.getName() );
csvOutput.writeRecord( signal.getComponent() );
csvOutput.writeRecord( Integer.toString( signal.getOccurance() ) );
csvOutput.writeRecord( Integer.toString( signal
.getSizewithHeader() ) );
csvOutput.writeRecord( Float.toString( signal
.getBandwidthWithHeader() ) );
csvOutput.writeRecord( Integer.toString( signal.getSize() ) );
csvOutput.writeRecord( Float.toString( signal.getBandwidth() ) );
csvOutput.endOfRecord();
}
}
catch ( IOException e )
{
logger.error( "Error in writing CSV file for messages", e );
}
finally
{
try
{
if ( csvOutput != null )
{
csvOutput.flush();
csvOutput.close();
}
messages.clear();
}
catch ( IOException ex )
{
ex.printStackTrace();
}
}
}
}
The signal class is
public class Signal
{
private String source;
private String name;
private String component;
private int occurance;
private int size;
private int sizeWithHeader;
private float bandwidth;
private float bandwidthwithHeader;
/**
* @param source
*/
public void setSource ( String source )
{
this.source = source;
}
/**
* @param name
*/
public void setName ( String name )
{
this.name = name;
}
/**
* @param component
*/
public void setComponent ( String component )
{
this.component = component;
}
/**
* @param occurance
*/
public void setOccurance ( int occurance )
{
this.occurance = occurance;
}
/**
* @param size
*/
public void setSize ( int size )
{
this.size = size;
}
/**
* @param bandwidth
*/
public void setBandwidth ( float bandwidth )
{
this.bandwidth = bandwidth;
}
/**
* @param sizeWithHeader
*/
public void setSizeWithHeader ( int sizeWithHeader )
{
this.sizeWithHeader = sizeWithHeader;
}
/**
* @param bandwidthwithHeader
*/
public void setBandwidthWithHeader ( float bandwidthwithHeader )
{
this.bandwidthwithHeader = bandwidthwithHeader;
}
/**
* @return String
*/
public String getSource ()
{
return this.source;
}
/**
* @return String
*/
public String getName ()
{
return this.name;
}
/**
* @return String
*/
public String getComponent ()
{
return this.component;
}
/**
* @return int
*/
public int getOccurance ()
{
return this.occurance;
}
/**
* @return int
*/
public int getSize ()
{
return this.size;
}
/**
* @return float
*/
public float getBandwidth ()
{
return this.bandwidth;
}
/**
* @return int
*/
public int getSizewithHeader ()
{
return this.sizeWithHeader;
}
/**
* @return float
*/
public float getBandwidthWithHeader ()
{
return this.bandwidthwithHeader;
}
}