Sau
Sau

Reputation: 2129

Unit testing for method in DAO

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.

Upvotes: 2

Views: 1679

Answers (1)

Thai Tran
Thai Tran

Reputation: 9935

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.

Upvotes: 2

Related Questions