【发布时间】:2014-06-25 12:48:22
【问题描述】:
如何使用 JUnit 测试来测试以下类。我是单元测试的新手,我只需要推动即可开始
public class ComponentComparator implements Comparator< Component >
{
@Override
public int compare ( final Component c1, final Component c2 )
{
if ( c1.getBandwidthWithHeader() > c2.getBandwidthWithHeader() )
{
return -1;
}
else if ( c1.getBandwidthWithHeader() < c2.getBandwidthWithHeader() )
{
return 1;
}
return 0;
}
}
部分组件类是,这个类没有构造函数
public class Component
{
private float bandwidthwithHeader;
public void setBandwidthWithHeader ( float bandwidthwithHeader )
{
this.bandwidthwithHeader = bandwidthwithHeader;
}
public float getBandwidthWithHeader ()
{
return this.bandwidthwithHeader;
}
}
【问题讨论】:
-
如果您尝试阅读 JUnit 简介,您应该会发现上面的内容足够简单,可以测试 vogella.com/tutorials/JUnit/article.html
-
提示:如果你的比较器比较两个空对象会发生什么?一个空对象?由于
TreeSet的性质,这些听起来像是可以尝试的好案例。
标签: java unit-testing junit junit4