四、实例:
下例演示了如何测试类NEListTable的ShowNETable()方法。其中注意的是,方法中调用了类QueryCtrl的queryNEList()方法。
待测类:
publicclass NEListTable
{
QueryCtrl ctrl = null;
publicvoid ShowNETable()
{
List neList = ctrl.queryNEList();
for(int i = 0;i<neList.size();i++)
{
//将neList转换为表格行
}
//显示表格...
}
}
publicclass QueryCtrl {
public List queryNEList()
{
returnnull;
}
}
测试类:
public class TestNEListTable extends TestCase
{
private NEListTable table = null;
private TestHelper helper = null;
public void testShowNETable()
{
Mock_QueryCtrl ctrl = new Mock_QueryCtrl();
helper.setObjectField(table,"ctrl",ctrl);//将Mock对象注入table
table.ShowNETable();
assertTrue(table.getRowCount()>0);
}
private class Mock_QueryCtrl extends QueryCtrl
{
public List queryNEList()
{
List neList = new ArrayList();
//返回你需要的数据...
return neList;
}
}
}