Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert that lists are equal with testng?

Tags:

java

testng

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

like image 273
jan Avatar asked Nov 02 '15 15:11

jan


People also ask

How do you assert equal a list?

We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods. In this first test, the size of both lists is compared before we check if the elements in both lists are the same. As both of these conditions return true, our test will pass.

How do you assert two lists in JUnit?

You can use assertEquals in junit. If the order of elements is different then it will return error. If you are asserting a model object list then you should override the equals method in the specific model.


1 Answers

There's no need for a separate method for List comparison. Two lists can be compared by org.testng.Assert#assertEquals(Object, Object).

If two lists a and b are non-null, the call Assert.assertEquals(a, b) means a.equals(b) will be called subsequently.

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 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.

like image 134
Tomas Pinos Avatar answered Sep 21 '22 09:09

Tomas Pinos