org.fest.assertions.api
Class ObjectAssert

java.lang.Object
  extended by org.fest.assertions.api.AbstractAssert<ObjectAssert,Object>
      extended by org.fest.assertions.api.ObjectAssert
All Implemented Interfaces:
Assert<ObjectAssert,Object>, Descriptable<ObjectAssert>, ExtensionPoints<ObjectAssert,Object>

public class ObjectAssert
extends AbstractAssert<ObjectAssert,Object>

Assertion methods for Objects.

To create a new instance of this class, invoke Assertions.assertThat(Object).

Author:
Yvonne Wang, Alex Ruiz, Nicolas François

Field Summary
 
Fields inherited from class org.fest.assertions.api.AbstractAssert
actual, myself
 
Constructor Summary
protected ObjectAssert(Object actual)
           
 
Method Summary
 ObjectAssert isInstanceOf(Class<?> type)
          Verifies that the actual Object is an instance of the given type.
 ObjectAssert isInstanceOfAny(Class<?>... types)
          Verifies that the actual Object is an instance of any of the given types.
 ObjectAssert isLenientEqualsToByAcceptingFields(Object other, 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.
 ObjectAssert isLenientEqualsToByIgnoringFields(Object other, String... fields)
          Assert that the actual object is lenient equals to given one by comparing actual and other fields except the given "ignored" fields.
 ObjectAssert isLenientEqualsToByIgnoringNullFields(Object other)
          Assert that the actual object is lenient equals to given one by only comparing actual and not null other fields.
 
Methods inherited from class org.fest.assertions.api.AbstractAssert
as, as, describedAs, describedAs, descriptionText, doesNotHave, equals, has, hashCode, is, isEqualTo, isIn, isIn, isNot, isNotEqualTo, isNotIn, isNotIn, isNotNull, isNotSameAs, isNull, isSameAs, usingComparator, usingDefaultComparator
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectAssert

protected ObjectAssert(Object actual)
Method Detail

isInstanceOf

public ObjectAssert isInstanceOf(Class<?> type)
Verifies that the actual Object is an instance of the given type.

Parameters:
type - the type to check the actual Object against.
Returns:
this assertion object.
Throws:
NullPointerException - if the given type is null.
AssertionError - if the actual Object is null.
AssertionError - if the actual Object is not an instance of the given type.

isInstanceOfAny

public ObjectAssert isInstanceOfAny(Class<?>... types)
Verifies that the actual Object is an instance of any of the given types.

Parameters:
types - the types to check the actual Object against.
Returns:
this assertion object.
Throws:
AssertionError - if the actual Object is null.
AssertionError - if the actual Object is not an instance of any of the given types.
NullPointerException - if the given array of types is null.
NullPointerException - if the given array of types contains nulls.

isLenientEqualsToByIgnoringNullFields

public ObjectAssert isLenientEqualsToByIgnoringNullFields(Object other)
Assert that the actual object is lenient equals to given one by only comparing actual and not null other 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 compare actual to.
Throws:
NullPointerException - if the actual type is null.
NullPointerException - if the other type 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.

isLenientEqualsToByAcceptingFields

public ObjectAssert isLenientEqualsToByAcceptingFields(Object other,
                                                       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.
 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 compare actual to.
fields - accepted fields for lenient equality.
Throws:
NullPointerException - if the actual type is null.
NullPointerException - if the other type 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.
org.fest.util.IntrospectionError - if a field does not exist in actual.

isLenientEqualsToByIgnoringFields

public ObjectAssert isLenientEqualsToByIgnoringFields(Object other,
                                                      String... fields)
Assert that the actual object is lenient equals to given one by comparing actual and other 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 compare actual to.
fields - ignored fields for lenient equality.
Throws:
NullPointerException - if the actual type is null.
NullPointerException - if the other type 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.


Copyright © 2007-2012 FEST (Fixtures for Easy Software Testing). All Rights Reserved.