对IO进行JUnit测试

我是新来的,也是junit测试的新手。 我有一个有两种方法的类,我想为它编写unit testing。 我不知道如何开始我阅读一些基本的教程,但我无法开始如何。 你们中的任何人都可以为我提供一些基本的骨架。

我的课是

public class CreateCSV { MyWriter csvOutput = null; public void createSortedSet ( final HashMap map, final long totalSize, final long totalSizewithHeader, File file ) { ArrayList messages = new ArrayList(); 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 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(); } } } } 

信号类是

  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; } } 

只是为了给你一个良好的开端

创建一个看起来像这样的测试类

 import junit.framework.TestCase; public class CreateCSVTest extends TestCase { CreateCSV csv = new CreateCSV(); public void testCreateCsv() { csv.createCSV("Pass an arraylist of type Signal", "pass a file"); assertEquals("add your asserts here", "add your asserts here"); } }