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
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
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 List
s 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
ande2
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 theList
interface.
See the JavaDocs of the List
interface.