Java Reference
In-Depth Information
--- Difference details ---
=> User<id=0, username=null, firstName="Jeffrey", lastName="Lebowsky",
telephones=[]>
=> User<id=0, username="ElDuderino", firstName="Jeffrey",
lastName="Lebowsky", telephones=[]>
username => null
username => "ElDuderino"
Not only did it find the difference, but it printed both brief and detailed messages of
what went wrong (and all assertion methods from this class behave this way).
If you want to ignore the fields that are
null
, you need to pass the
Reflection-
ComparatorMode.IGNORE_DETAILS
mode as a parameter, such as
assertReflection-
Equals(user1,
user2,
IGNORE_DETAILS)
. Be aware that the order is important
here, because fields are ignored only when their value is
null
in the
expected
param-
eter. Calling
assertReflectionEquals(user2,
user1,
IGNORE_DETAILS)
with these
same
User
instances would fail, whereas
assertReflectionEquals(user1,
user2,
IGNORE_DETAILS)
would pass.
The other two comparison modes are
LENIENT_DATES
, which ignores in the
comparison any field that's a
java.util.Date
, and
LENIENT_ORDER
, which is rele-
vant only when comparing collections. Because the lenient comparison is common,
most of the
ReflectionAssert
assertReflection
XXX
()
methods offer a counter-
part called
assertLenient
XXX
()
that automatically includes
IGNORE_DETAILS
and
LENIENT_ORDER
, such as
assertLenientEquals(Object
expect,
Object
actual)
.
The idea behind this behavior is that in many cases the functionality being tested
doesn't fill every field of an object, and having to compare all of them would
require a lot more work. That's particularly useful in database access tests, such as
in a
getLoginAndPassword()
method where the user's table has dozens of fields but
only two of them are filled and need to be compared.
These methods can also be used to compare collections. For instance, using the
lists defined a few examples ago and calling
assertReflectionEquals(LIST1,
LIST4)
results in the following:
junit.framework.AssertionFailedError: Found following differences:
[4]
=> 23
=> 108
[5]
=> 42
=> 23
--- Difference details ---
=> [4, 8, 15, 16, 23, 42]
=> [4, 8, 15, 16, 108, 23]
[4] => 23
[4] => 108