IO的JUnit测试 [英] JUnit testing for IO

查看:131
本文介绍了IO的JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这里的新手,也是junit测试的新手.我有一个带有两种方法的类,我想为此编写单元测试.我不确定如何开始阅读了一些基本教程,但是却无法开始.谁能给我一些基本的框架.

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.

我的课是

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

信号类别为

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

推荐答案

只为您起步

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

Create your test class that looks something like this

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

}

这篇关于IO的JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆