java - JUnit testing for IO -
i new here , new junit testing. have class 2 methods , want write unit tests it. not sure how start read basic tutorials not able start how. can of provide me basic skeleton start with.
my class
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(); ( 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 specifies open appending csvoutput = new mywriter( new filewriter( file, false ), ',' ); // create header csv csvoutput.writerecord( "message source" ); csvoutput.writerecord( "message name" ); csvoutput.writerecord( "component" ); csvoutput.writerecord( "occurance" ); csvoutput.writerecord( "message payload header" ); csvoutput.writerecord( "bandwidth(with header %)" ); csvoutput.writerecord( "message payload" ); csvoutput.writerecord( "bandwidth(%)" ); csvoutput.endofrecord(); ( 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 messages", e ); } { try { if ( csvoutput != null ) { csvoutput.flush(); csvoutput.close(); } messages.clear(); } catch ( ioexception ex ) { ex.printstacktrace(); } } } }
the signal class
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; } }
just give head start
create test class looks this
import junit.framework.testcase; public class createcsvtest extends testcase { createcsv csv = new createcsv(); public void testcreatecsv() { csv.createcsv("pass arraylist of type signal", "pass file"); assertequals("add asserts here", "add asserts here"); } }
Comments
Post a Comment