2

In the DAO a query returns me and List<Object[]>

I have made a been ABC which has

protected String Name;
protected Integer AGE;

and the getters and setters for it.

This is my DAO Method

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;

How can I Write JUNit test for this method. Through Junit I can check whether the return list is empty or not but what if I want to check what is there in the list.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Sau
  • 2,109
  • 6
  • 21
  • 22
  • What is result? Do you want to make a real call to the db or mock it? – smk Mar 08 '13 at 06:20
  • I'm new to Junit please suggest me which one is good by mock what i understand is that its a dummy values right. – Sau Mar 08 '13 at 06:30
  • result is firstname 25 then secondname 30 and thirdname 45 – Sau Mar 08 '13 at 06:41
  • see http://stackoverflow.com/questions/1512689/easy-way-to-compare-arraylists-for-equality-using-junit – brabec Mar 08 '13 at 08:05

1 Answers1

2

You can use assertEquals to check the returned values if they are corrected or not

@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   

}

Usually, we will prepare a test data set (say, in your case, "firstname" or 25), so, when running, we know the expected result of each unit test.

Thai Tran
  • 9,815
  • 7
  • 43
  • 64
  • Yes its work for me I really thankful to you for helping me out. i can't vote up because my reputation is only 6 . but this is a Right solution to my Question thanks once again. – Sau Mar 08 '13 at 08:34