Reputation: 400
When comparing two objects using assertEquals()
does it considering looking into the internal structure viz. the properties of the objects?
Suppose I have a class A like following:
public class A {
private int ID;
private String name;
private String address;
}
And suppose the supplied object's (that is to be compared to an object of A
) properties are in a different order, then what will assertEquals()
do? Is there a robust method to do it the other way?
Upvotes: 6
Views: 15064
Reputation: 10739
@JimGarrison is quite right - assertEquals()
will just call the equals()
method on your objects to determine equality.
To answer your question, "Is there a robust method to do it the other way?",
if you can't properly implement the equals()
method on your class for whatever reason, and you want to evaluate the equality of objects based on the values of their fields, consider using EqualsBuilder's reflectionEquals()
method. It is fairly robust, and allows you to exclude whatever fields you want.
To answer your other question, "suppose the supplied object's properties are in a different order, then what will assertEquals() do?", all it will do is call the equals()
method on the other instance. For example, if you call assertEquals(a, b)
, then that will eventually call a.equals(b)
. However, if you call assertEquals(b, a)
, then that will eventually call b.equals(a)
.
I hope that helps.
Upvotes: 7
Reputation: 86774
Assert.assertEquals()
will use the class' equals()
method.
If your class overrides equals()
to do a deep comparison then that's what will be used. If you don't override equals()
then you'll get Object#equals()
based on identity equality only.
In other words, YOU decide what equals()
means.
Upvotes: 6