views:

26

answers:

2

I am writing a test case where I do send a list of Strings to be saved in the database. Then I will retrieve those from database and has to verify that everything is fine.

I have written a

assertNotNull(list) 
assertEquals(listSize, response.listSize())

However I want to verify the actual contents are also same. But my assertEquals is failing since the list of strings are not in the same order when they are returned.

How do you verify this type of thing usually?

+2  A: 

Er... why not just force the ordering by creating the initial list alphabetically (or use a sort) and then use the ORDER BY clause in SQL?

That said, you may need to iterate through the elements in the list and compare them (as the keys may also be different in your original list and that retrieved from the database).

middaparka
I have done this by using Arrays.sort() and Arrays.equals()
Reddy
+1  A: 

assuming you have a List expected, which are the strings you expect, you can do

assertTrue (response.containsAll(expected))

that combined with your size verification makes sure that the list is complete and doesn't contain extras.

hvgotcodes