【问题标题】:Unit testing for method in DAODAO中方法的单元测试
【发布时间】:2013-08-16 02:50:22
【问题描述】:

在 DAO 中,一个查询返回我和List<Object[]>

我做了一个 ABC

protected String Name;
protected Integer AGE;

以及它的 getter 和 setter。

这是我的 DAO 方法

List<ABC> list = new ArrayList<ABC>();
if(result!=null && !result.isEmpty())
{
    Iterator dataIter = result.iterator();
    while(dataIter.hasNext()) 
    {
    Object[] row = (Object[]) dataIter.next();
    ABC abc = new ABC();
    abc.setName((String)row[0]);
    abc.setAGE((Integer)row[1]);
    list.add(abc);
    }
}

return list;

我怎样才能为此方法编写 JUNit 测试。通过 Junit 我可以检查返回列表是否为空,但是如果我想检查列表中有什么。

【问题讨论】:

  • 什么是结果?你想真正调用数据库还是模拟它?
  • 我是 Junit 的新手,请通过模拟告诉我哪个好,我的理解是它是一个虚拟值。
  • 结果是 firstname 25 然后 secondname 30 和 thirdname 45

标签: java junit junit4


【解决方案1】:

您可以使用assertEquals 来检查返回的值是否正确

@Test
public void testListOfABC() throws DAOException {
    // Declare your DAO 


    List<ABC> lstABC = dao.yourFunction();

    // check if it is null
    assertNotNull(lstABC);

    // check if the returned list have enough values 
    assertEquals(lstABC.size(), 3);

    // check if each value is corrected 
    assertEquals(lstABC.get(0).getName(), "firstname");
    assertEquals(lstABC.get(0).getAGE(), 25);
    // and so on   

}

通常,我们会准备一个测试数据集(例如,在您的情况下,“名字”或 25),因此,在运行时,我们知道每个单元测试的预期结果。

【讨论】:

  • 是的,它为我工作,我真的很感谢你帮助我。我不能投票,因为我的声誉只有 6 。但这是我问题的正确解决方案,再次感谢。
猜你喜欢
  • 2011-04-11
  • 2013-07-09
  • 2012-12-22
  • 2013-08-26
  • 1970-01-01
  • 2015-10-07
  • 1970-01-01
  • 2012-05-14
  • 2011-12-31
相关资源
最近更新 更多