How to assert that lists are equal with testng?

I found an answer for junit, but you need a solution for testng. Any ideas more useful for writing your own loop?

+6
source share
2 answers

There is no need for a separate List comparison method. The two lists can be compared using org.testng.Assert#assertEquals(Object, Object) .

If the two lists a and b are not null , calling Assert.assertEquals(a, b) means a.equals(b) will be called later.

And java.util.List#equals is what you need as described in 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 are the same size, and all the corresponding pairs of elements in 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 correctly with various implementations of the List interface.

+9
source

If you don't mind adding a new dependency, I suggest taking a look at AssertJ , which provides a lot of amazing statement on collections.

+1
source

All Articles