tags:

views:

35

answers:

1

Hi, How to assert list in Junit test case ? I mean not only the size of the list but also the contents of the list.

Thanks, Kamal

+2  A: 
List<E> a = resultFromTest();
List<E> expected = Arrays.asList(new E(), new E(), ...);
asssertTrue("Expected 'a' and 'expected' to be equal."+
            "\n  'a'        = "+a+
            "\n  'expected' = "+expected, 
            expected.equals(a));

For the record, as @Paul mentioned in his comment to this answer, two Lists are equal:

if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

See the JavaDocs of the List interface.

Bart Kiers
So you mean expected.equals(a) will take care of asserting the objects that the list is holding ?
Kamal
From List javadoc: Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.
Paul McKenzie
@Kamal, yes (see Paul's comment and my edit).
Bart Kiers
This alas provides less than helpful error message. I have found it better to write a utility class which performs a loop so you can see which elements are different.
mlk
@mlk, perhaps, but I'm hesitant to write a custom utility method for such a thing. What about the error message I edited just now?
Bart Kiers
@mlk I agree that it might be better to write a loop to test each element so you know exactly what is different. It depends on what types of objects are in the list. If they are Strings, then just say "a="+a should be fine, but if they are complex objects (other lists, or something that doesn't have a good toString implementation), it might be easier to test them individually
Matt N
So in case the list are having same objects, but in different order, this expected.equals(a) will return false.
Kamal
@Kamal, yes, that is correct.
Bart Kiers
Bart/Matt - The solution I use is I have /the loop/ but if the loop finds a difference it generates a string of the array using Apache Commons ReflectionToStringBuilder for the elements. It then does a string assert on the two generated strings (as this is colour coded in Eclipses JUnit runner) followed by a simple fail should the string assert pass. It is not a solution for every occasion but it works nicely for many.
mlk