Package org.fest.assertions.api
Class ObjectAssert<T>
- java.lang.Object
-
- org.fest.assertions.api.AbstractAssert<ObjectAssert<T>,T>
-
- org.fest.assertions.api.ObjectAssert<T>
-
- All Implemented Interfaces:
Assert<ObjectAssert<T>,T>
,Descriptable<ObjectAssert<T>>
,ExtensionPoints<ObjectAssert<T>,T>
public class ObjectAssert<T> extends AbstractAssert<ObjectAssert<T>,T>
Assertion methods forObject
s.To create a new instance of this class, invoke
.Assertions.assertThat(Object)
- Author:
- Yvonne Wang, Alex Ruiz, Nicolas François, Mikhail Mazursky
-
-
Field Summary
-
Fields inherited from class org.fest.assertions.api.AbstractAssert
actual, myself
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ObjectAssert(T actual)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ObjectAssert<T>
isEqualsToByComparingFields(T other)
Assert that the actual object is equals fields by fields to another object, inherited fields are taken into account.ObjectAssert<T>
isLenientEqualsToByAcceptingFields(T other, java.lang.String... fields)
Assert that the actual object is lenient equals to given one by only comparing actual and other on the given "accepted" fields only ("accepted" fields can be inherited fields).ObjectAssert<T>
isLenientEqualsToByIgnoringFields(T other, java.lang.String... fields)
Assert that the actual object is lenient equals to given one by comparing actual and other fields (including inherited fields) except the given "ignored" fields.ObjectAssert<T>
isLenientEqualsToByIgnoringNullFields(T other)
Assert that the actual object is lenient equals to given one by comparing only actual and not null other fields (including inherited fields).-
Methods inherited from class org.fest.assertions.api.AbstractAssert
as, as, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, getWritableAssertionInfo, has, hashCode, hasSameClassAs, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, overridingErrorMessage, usingComparator, usingDefaultComparator
-
-
-
-
Constructor Detail
-
ObjectAssert
protected ObjectAssert(T actual)
-
-
Method Detail
-
isLenientEqualsToByIgnoringNullFields
public ObjectAssert<T> isLenientEqualsToByIgnoringNullFields(T other)
Assert that the actual object is lenient equals to given one by comparing only actual and not null other fields (including inherited fields).It means that if an actual field is not null and the corresponding field in other is null, field will be ignored by lenient comparison, but the inverse will make assertion fail (null field in actual, not null in other).
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).isLenientEqualsToByIgnoringNullFields(mysteriousHobbit); //=> OK // ... but the lenient equality is not reversible ! assertThat(mysteriousHobbit).isLenientEqualsToByIgnoringNullFields(frodo); //=> FAIL
- Parameters:
other
- the object to compareactual
to.- Throws:
java.lang.NullPointerException
- if the actual type isnull
.java.lang.NullPointerException
- if the other type isnull
.java.lang.AssertionError
- if the actual and the given object are not lenient equals.java.lang.AssertionError
- if the other object is not an instance of the actual type.
-
isLenientEqualsToByAcceptingFields
public ObjectAssert<T> isLenientEqualsToByAcceptingFields(T other, java.lang.String... fields)
Assert that the actual object is lenient equals to given one by only comparing actual and other on the given "accepted" fields only ("accepted" fields can be inherited fields).Example:
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT); TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT); // frodo and sam both are hobbits, so they are lenient equals on race assertThat(frodo).isLenientEqualsToByAcceptingFields(sam, "race"); // => OK // ... but not when accepting name and race assertThat(frodo).isLenientEqualsToByAcceptingFields(sam, "name", "race"); // => FAIL
- Parameters:
other
- the object to compareactual
to.fields
- accepted fields for lenient equality.- Throws:
java.lang.NullPointerException
- if the actual type isnull
.java.lang.NullPointerException
- if the other type isnull
.java.lang.AssertionError
- if the actual and the given object are not lenient equals.java.lang.AssertionError
- if the other object is not an instance of the actual type.org.fest.util.IntrospectionError
- if a field does not exist in actual.
-
isLenientEqualsToByIgnoringFields
public ObjectAssert<T> isLenientEqualsToByIgnoringFields(T other, java.lang.String... fields)
Assert that the actual object is lenient equals to given one by comparing actual and other fields (including inherited fields) except the given "ignored" fields.Example: TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT); TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT); // frodo and sam both are lenient equals ignoring name and age since only remaining property is race and frodo and sam both are HOBBIT assertThat(frodo).isLenientEqualsToByIgnoringFields(sam, "name", "age"); //=> OK // ... but they are not lenient equals if only age is ignored because their names differ. assertThat(frodo).isLenientEqualsToByIgnoringFields(sam, "age"); //=> FAIL
- Parameters:
other
- the object to compareactual
to.fields
- ignored fields for lenient equality.- Throws:
java.lang.NullPointerException
- if the actual type isnull
.java.lang.NullPointerException
- if the other type isnull
.java.lang.AssertionError
- if the actual and the given object are not lenient equals.java.lang.AssertionError
- if the other object is not an instance of the actual type.
-
isEqualsToByComparingFields
public ObjectAssert<T> isEqualsToByComparingFields(T other)
Assert that the actual object is equals fields by fields to another object, inherited fields are taken into account.This can be handy if
equals
implementation of objects to compare does not suit you.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); // Fail if equals has not been overriden in TolkienCharacter // frodo and frodoClone are equals by comparing fields assertThat(frodo).isEqualsToByComparingFields(frodoClone); // OK
- Parameters:
other
- the object to compareactual
to.- Throws:
java.lang.NullPointerException
- if the actual type isnull
.java.lang.NullPointerException
- if the other type isnull
.java.lang.AssertionError
- if the actual and the given object are not equals fields by fields.java.lang.AssertionError
- if the other object is not an instance of the actual type.
-
-