org.assertj.core.api
Class AbstractObjectAssert<S extends AbstractObjectAssert<S,A>,A>

java.lang.Object
  extended by org.assertj.core.api.AbstractAssert<S,A>
      extended by org.assertj.core.api.AbstractObjectAssert<S,A>
Type Parameters:
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.
All Implemented Interfaces:
Assert<S,A>, Descriptable<S>, ExtensionPoints<S,A>
Direct Known Subclasses:
ObjectAssert

public abstract class AbstractObjectAssert<S extends AbstractObjectAssert<S,A>,A>
extends AbstractAssert<S,A>

Base class for all implementations of assertions for Objects.

Author:
Yvonne Wang, Alex Ruiz, Nicolas François, Mikhail Mazursky, Joel Costigliola

Field Summary
 
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself
 
Constructor Summary
protected AbstractObjectAssert(A actual, Class<?> selfType)
           
 
Method Summary
 S isEqualsToByComparingFields(A other)
          Deprecated. : use isEqualToComparingFieldByField(Object) instead.
 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).
 S isLenientEqualsToByAcceptingFields(A other, String... fields)
          Deprecated. : use isEqualToComparingOnlyGivenFields(Object, String...) instead.
 S isLenientEqualsToByIgnoringFields(A other, String... fields)
          Deprecated. : use isEqualToIgnoringGivenFields(Object, String...) instead.
 S isLenientEqualsToByIgnoringNullFields(A other)
          Deprecated. : use isEqualToIgnoringNullFields(Object) instead.
 
Methods inherited from class org.assertj.core.api.AbstractAssert
as, as, 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
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractObjectAssert

protected AbstractObjectAssert(A actual,
                               Class<?> selfType)
Method Detail

isEqualToIgnoringNullFields

public 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).

It means that if an actual field is not null and the corresponding field in other is null, this field will be ignored in comparison, but the opposite will make assertion fail (null field in actual, not null in other).

Note that only accessible fields values are compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.
Moreover comparison is not recursive, if one of the field is an Object, it will be compared to the other field using its equals method.

 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
 
 

Parameters:
other - the object to compare actual to.
Throws:
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.

isLenientEqualsToByIgnoringNullFields

public S isLenientEqualsToByIgnoringNullFields(A other)
Deprecated. : use isEqualToIgnoringNullFields(Object) instead.


isEqualToComparingOnlyGivenFields

public 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). This can be handy if equals implementation of objects to compare does not suit you.

Note that 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
 
 

Parameters:
other - the object to compare actual to.
fieldsUsedInComparison - accepted fieldsUsedInComparison for lenient equality.
Throws:
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.

isLenientEqualsToByAcceptingFields

public S isLenientEqualsToByAcceptingFields(A other,
                                            String... fields)
Deprecated. : use isEqualToComparingOnlyGivenFields(Object, String...) instead.


isEqualToIgnoringGivenFields

public 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). This can be handy if equals implementation of objects to compare does not suit you.

Note that only accessible fields values are compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.
Moreover comparison is not recursive, if one of the field is an Object, it will be compared to the other field using its equals method.

 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
 
 

Parameters:
other - the object to compare actual to.
fieldsToIgnore - ignored fieldsToIgnore for lenient equality.
Throws:
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.

isLenientEqualsToByIgnoringFields

public S isLenientEqualsToByIgnoringFields(A other,
                                           String... fields)
Deprecated. : use isEqualToIgnoringGivenFields(Object, String...) instead.


isEqualToComparingFieldByField

public 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). This can be handy if equals implementation of objects to compare does not suit you.

Note that only accessible fields values are compared, accessible fields include directly accessible fields (e.g. public) or fields with an accessible getter.
Moreover comparison is not recursive, if one of the field is an Object, it will be compared to the other field using its equals method.

 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);
 
 

Parameters:
other - the object to compare actual to.
Throws:
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.

isEqualsToByComparingFields

public S isEqualsToByComparingFields(A other)
Deprecated. : use isEqualToComparingFieldByField(Object) instead.



Copyright © 2013–2015 AssertJ. All rights reserved.