S
- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation"
for more details.A
- the type of the "actual" value.public abstract class AbstractObjectAssert<S extends AbstractObjectAssert<S,A>,A> extends AbstractAssert<S,A>
Object
s.actual, info, myself
Modifier | Constructor and Description |
---|---|
protected |
AbstractObjectAssert(A actual,
Class<?> selfType) |
Modifier and Type | Method and Description |
---|---|
S |
isEqualToComparingFieldByField(A other)
Assert that the actual object is equal to the given object based on a field by field comparison (including
inherited fields).
|
S |
isEqualToComparingOnlyGivenFields(A other,
String... fieldsUsedInComparison)
Assert that the actual object is equal to given one using a field by field comparison on the given fields only
(fields can be inherited fields or nested fields).
|
S |
isEqualToIgnoringGivenFields(A other,
String... fieldsToIgnore)
Assert that the actual object is equal to the given one by comparing their fields except for the given ones
(inherited fields are taken into account).
|
S |
isEqualToIgnoringNullFields(A other)
Assert that the actual object is equal to the given one by comparing actual's fields with not null other's
fields only (including inherited fields).
|
as, as, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, inBinary, inHexadecimal, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, overridingErrorMessage, usingComparator, usingDefaultComparator, withThreadDumpOnError
public S isEqualToIgnoringNullFields(A other)
equals
method.
Private fields are used in comparison but this can be disabled using
Assertions.setAllowComparingPrivateFields(boolean)
, if disabled only accessible fields values are
compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.
Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter mysteriousHobbit = new TolkienCharacter(null, 33, HOBBIT);
// Null fields in other/expected object are ignored, the mysteriousHobbit has null name thus name is ignored
assertThat(frodo).isEqualToIgnoringNullFields(mysteriousHobbit); // OK
// ... but the lenient equality is not reversible !
assertThat(mysteriousHobbit).isEqualToIgnoringNullFields(frodo); // FAIL
other
- the object to compare actual
to.NullPointerException
- if the actual or other object is null
.AssertionError
- if the actual and the given object are not lenient equals.AssertionError
- if the other object is not an instance of the actual type.public S isEqualToComparingOnlyGivenFields(A other, String... fieldsUsedInComparison)
equals
implementation of
objects to compare
does not suit you.
Note that comparison is not recursive, if one of the field is an Object, it will be compared to the other
field using its equals
method.
Private fields are used in comparison but this can be disabled using
Assertions.setAllowComparingPrivateFields(boolean)
, if disabled only accessible fields values are
compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.
Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);
// frodo and sam both are hobbits, so they are equals when comparing only race
assertThat(frodo).isEqualToComparingOnlyGivenFields(sam, "race"); // OK
// they are also equals when comparing only race name (nested field).
assertThat(frodo).isEqualToComparingOnlyGivenFields(sam, "race.name"); // OK
// ... but not when comparing both name and race
assertThat(frodo).isEqualToComparingOnlyGivenFields(sam, "name", "race"); // FAIL
other
- the object to compare actual
to.fieldsUsedInComparison
- accepted fieldsUsedInComparison for lenient equality.NullPointerException
- if the actual or other is null
.AssertionError
- if the actual and the given object are not lenient equals.AssertionError
- if the other object is not an instance of the actual type.IntrospectionError
- if a field does not exist in actual.public S isEqualToIgnoringGivenFields(A other, String... fieldsToIgnore)
equals
implementation of objects to
compare does not suit you.
Note that comparison is not recursive, if one of the field is an Object, it will be compared to the other
field using its equals
method.
Private fields are used in comparison but this can be disabled using
Assertions.setAllowComparingPrivateFields(boolean)
, if disabled only accessible fields values are
compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.
Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);
// frodo and sam are equals when ignoring name and age since the only remaining field is race which they share as HOBBIT.
assertThat(frodo).isEqualToIgnoringGivenFields(sam, "name", "age"); // OK
// ... but they are not equals if only age is ignored as their names differ.
assertThat(frodo).isEqualToIgnoringGivenFields(sam, "age"); // FAIL
other
- the object to compare actual
to.fieldsToIgnore
- ignored fieldsToIgnore for lenient equality.NullPointerException
- if the actual or given object is null
.AssertionError
- if the actual and the given object are not lenient equals.AssertionError
- if the other object is not an instance of the actual type.public S isEqualToComparingFieldByField(A other)
equals
implementation of objects to compare does not suit you.
Note that comparison is not recursive, if one of the field is an Object, it will be compared to the other
field using its equals
method.
Private fields are used in comparison but this can be disabled using
Assertions.setAllowComparingPrivateFields(boolean)
, if disabled only accessible fields values are
compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.
Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);
// Fail if equals has not been overriden in TolkienCharacter as equals default implementation only compares references
assertThat(frodo).isEqualsTo(frodoClone);
// frodo and frodoClone are equals when doing a field by field comparison.
assertThat(frodo).isEqualToComparingFieldByField(frodoClone);
other
- the object to compare actual
to.NullPointerException
- if the actual or given object is null
.AssertionError
- if the actual and the given object are not equals field by field.AssertionError
- if the other object is not an instance of the actual type.Copyright © 2013-2015 AssertJ. All Rights Reserved.