How to assert that lists are equal with testng? - java

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

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.

Related

How to comapre two list of hibernate entities are equal

I have list of 2 hibernate entities.
I need to compare them. When I use equals() it always gives false as its reference is different.
E1.equals(E2)
It's because you're taking a reference to the List object only when evaluating equality. You would have to iterate through the collections to check (assuming the ordering is important):
E1.size() == E2.size() && IntStream.range(0, E1.size())
.allMatch(index -> E1.get(index).equals(E2.get(index)));
P.S.1: Variable names are written lowercase in java.
P.S.2: Make sure you're defining equals and hashCode properly. By default same entity is not equal in different states (managed, detached).

difference between a.equals(b) and array.equals(a,b) [duplicate]

This question already has answers here:
equals vs Arrays.equals in Java
(9 answers)
Closed 7 years ago.
I have run this small program:
String[] a = {"a","b"};
String[] b = {"a","b"};
if (a.equals(b)){
System.out.println("woop");
}else{
System.out.println("doh!");
}
if (Arrays.equals(a, b)){
System.out.println("woop");
}else{
System.out.println("doh!");
}
The output of the program is "doh! woop".
I get the difference between the == and .equals but what is the difference between these equals operations?
TL;DR
equals compares array instances, Arrays.equals compares array elements.
Full Explanation
Arrays do not implement equals. As a result, they hand the method off to Object which is implemented as follows.
public boolean equals(Object obj) {
return (this == obj);
}
Thus, equals on an array is the same as ==.
Arrays.equals has the following description. It compares the elements of the array.
Returns true if the two specified arrays of longs are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.
Arrays.equals(Object[] a, Object[] a2)...
returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)). In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.
In short, all your Strings are compared through equals.
See API.
Object.equals, which is invoked when comparing two arrays with the array1.equals(array2) idiom, compares the reference, just like ==.
See source for java.lang.Object:
public boolean equals (Object o) {
return this == o;
}
The difference is that a.equals(b) will use == under the hood, since arrays don't override the Object.equals() method. You use this to test for instance equality between arrays.
So in your case, a.equals(b) will return false, since the two arrays are different instances.
Instead, Arrays.equals() actually compares what's inside the arrays. If the arrays contain the same values, then Arrays.equals() returns true. You use this to test for semantic equality between arrays.
With arrays, a.equals(b) is the same as a == b. This is checking if the two array instances are the same. Arrays.equals(a,b) takes the time to compare each element of both arrays and checks if the elements are equal, which is different than checking if the containers themselves are equal.
A quick analogy: Let's say a and b are buckets. Asking if the two buckets are the same is not the same as asking if what is in the buckets is the same.

Different reference stacks return equals true

towers=new ArrayList<>();
towers.add(new Stack<Integer>());
towers.add(new Stack<Integer>());
towers.add(new Stack<Integer>());
Why would this return true?
towers.get(1).equals(towers.get(2))
Did they somehow implement equals for stacks to check elements instead of the default behaviour which is reference
Because of the equals contract for stacks.
http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
Follow that to:
http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html#equals(java.lang.Object)
To wit:
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.

TreeSet Comperator

I used a TreeSet with a self written Comparator. Now when I'm adding elements to the TreeSet and the Comparator's compare methods returns 0, it seems like the TreeSet contains only one of the Object with equal ranking.
I didn't see that this behaviour is documented in the javadocs. Maybe I miss something. Can you confirm this behaviour?
I edited the Comparator. Now it never returns 0 and the TreeSet contains all the Objects with equal ranking.
Is that the way it has to be, if I want to have multiple Objects with equal ranking?
That's the way it has to be, as a set is defined as including equal objects only once.
When your Comparator returns 0, two objects are considered equal, therefore only one (probably the first) of all equal objects is included in the set.
Yes, this is documented in the JavaDoc for TreeSet:
Note that the ordering maintained by a
set (whether or not an explicit
comparator is provided) must be
consistent with equals if it is to
correctly implement the Set interface.
(See Comparable or Comparator for a
precise definition of consistent with
equals.) This is so because the Set
interface is defined in terms of the
equals operation, but a TreeSet
instance performs all element
comparisons using its compareTo (or
compare) method, so two elements that
are deemed equal by this method are,
from the standpoint of the set, equal.
The behavior of a set is well-defined
even if its ordering is inconsistent
with equals; it just fails to obey the
general contract of the Set interface. (Emphasis mine)
If you want a sorted collection that can hold multiple objects which are equal to each other, then the TreeMultiset from Google Collections would probably do the trick.

The **equals** in Set interface

As we know two Set instances are equal iff they contain the same elements, BUT is it possible to have the same element in two different sets (Set interface can not contain duplicate element)?
Distinct sets have no affect on one another. Set A can contain '123456', and Set B can also contain '123456' - the prohibition on duplicates is for a single instance, not across instances.
Equality for two sets implies that their contents are identical.
You are not comparing the elements inside a Set with each other to detect if they are equal, you are comparing the Elements of Set1 with the elements of Set2.
the equals method is described pretty well here.

Categories

Resources