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 actual object is equal to the given object based on a property/field by property/field comparison (including
inherited ones).
|
S |
isEqualToComparingOnlyGivenFields(A other,
String... propertiesOrFieldsUsedInComparison)
Assert that the actual object is equal to the given one using a property/field by property/field comparison on the given properties/fields only
(fields can be inherited fields or nested fields).
|
S |
isEqualToIgnoringGivenFields(A other,
String... propertiesOrFieldsToIgnore)
Assert that the actual object is equal to the given one by comparing their properties/fields except for the given ones
(inherited ones are taken into account).
|
S |
isEqualToIgnoringNullFields(A other)
Assert that the actual object is equal to the given one by comparing actual's properties/fields with other's
not null properties/fields only (including inherited ones).
|
as, as, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasToString, 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, withFailMessage, withThreadDumpOnError
public S isEqualToIgnoringNullFields(A other)
equals
method.
If an object has a field and a property with the same name, the property value will be used over the field.
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.
The objects to compare can be of different types but the properties/fields used in comparison must exist in both,
for example if actual object has a name String field, it is expected other object to also have one.
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.IntrospectionError
- if one of actual's field to compare can't be found in the other object.public S isEqualToComparingOnlyGivenFields(A other, String... propertiesOrFieldsUsedInComparison)
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.
If an object has a field and a property with the same name, the property value will be used over the field.
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.
The objects to compare can be of different types but the properties/fields used in comparison must exist in both,
for example if actual object has a name String field, it is expected the other object to also have one.
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.propertiesOrFieldsUsedInComparison
- properties/fields used in comparison.NullPointerException
- if the actual or other is null
.AssertionError
- if the actual and the given objects are not equals property/field by property/field on given fields.IntrospectionError
- if one of actual's property/field to compare can't be found in the other object.IntrospectionError
- if a property/field does not exist in actual.public S isEqualToIgnoringGivenFields(A other, String... propertiesOrFieldsToIgnore)
equals
implementation of objects to compare does not suit you.
Note that comparison is not recursive, if one of the property/field is an Object, it will be compared to the other
field using its equals
method.
If an object has a field and a property with the same name, the property value will be used over the field.
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.
The objects to compare can be of different types but the properties/fields used in comparison must exist in both,
for example if actual object has a name String field, it is expected the other object to also have one.
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.propertiesOrFieldsToIgnore
- ignored properties/fields to ignore in comparison.NullPointerException
- if the actual or given object is null
.AssertionError
- if the actual and the given objects are not equals property/field by property/field after ignoring given fields.IntrospectionError
- if one of actual's property/field to compare can't be found in the other object.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.
If an object has a field and a property with the same name, the property value will be used over the field.
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.
The objects to compare can be of different types but the properties/fields used in comparison must exist in both,
for example if actual object has a name String field, it is expected the other object to also have one.
Example:
// equals not overridden in TolkienCharacter
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);
// Fail as equals compares object 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 objects are not equals property/field by property/field.IntrospectionError
- if one of actual's property/field to compare can't be found in the other object.Copyright © 2013-2015 AssertJ. All Rights Reserved.