org.fest.assertions
Class ObjectAssert

java.lang.Object
  extended by org.fest.assertions.Assert
      extended by org.fest.assertions.GenericAssert<Object>
          extended by org.fest.assertions.ObjectAssert

public class ObjectAssert
extends GenericAssert<Object>

Understands assertion methods for objects. To create a new instance of this class use the method Assertions.assertThat(Object).

Author:
Yvonne Wang, Alex Ruiz

Field Summary
 
Fields inherited from class org.fest.assertions.GenericAssert
actual
 
Constructor Summary
protected ObjectAssert(Object actual)
          Creates a new ObjectAssert.
 
Method Summary
 ObjectAssert as(Description description)
          Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails.
 ObjectAssert as(String description)
          Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails.
 ObjectAssert describedAs(Description description)
          Alias for GenericAssert.as(Description), since "as" is a keyword in Groovy.
 ObjectAssert describedAs(String description)
          Alias for GenericAssert.as(String), since "as" is a keyword in Groovy.
 ObjectAssert doesNotSatisfy(Condition<Object> condition)
          Verifies that the actual Object does not satisfy the given condition.
 ObjectAssert is(Condition<Object> condition)
          Alias for satisfies(Condition).
 ObjectAssert isEqualTo(Object expected)
          Verifies that the actual Object is equal to the given one.
 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 isNot(Condition<Object> condition)
          Alias for doesNotSatisfy(Condition).
 ObjectAssert isNotEqualTo(Object other)
          Verifies that the actual Object is not equal to the given one.
 ObjectAssert isNotNull()
          Verifies that the actual Object is not null.
 ObjectAssert isNotSameAs(Object other)
          Verifies that the actual Object is not the same as the given one.
 ObjectAssert isSameAs(Object expected)
          Verifies that the actual Object is the same as the given one.
 ObjectAssert overridingErrorMessage(String message)
          Replaces the default message displayed in case of a failure with the given one.
 ObjectAssert satisfies(Condition<Object> condition)
          Verifies that the actual Object satisfies the given condition.
 
Methods inherited from class org.fest.assertions.GenericAssert
assertDoesNotSatisfy, assertEqualTo, assertIs, assertIsNot, assertNotEqualTo, assertNotNull, assertNotSameAs, assertSameAs, assertSatisfies, isNull
 
Methods inherited from class org.fest.assertions.Assert
customErrorMessage, description, description, description, equals, fail, fail, failIfCustomMessageIsSet, failIfCustomMessageIsSet, failure, formattedErrorMessage, hashCode, rawDescription, replaceDefaultErrorMessagesWith
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectAssert

protected ObjectAssert(Object actual)
Creates a new ObjectAssert.

Parameters:
actual - the target to verify.
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:
AssertionError - if the actual Object is null.
AssertionError - if the actual Object is not an instance of the given type.
NullPointerException - if the given type is null.

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.

as

public ObjectAssert as(String description)
Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

For example:

 assertThat(val).as("name").isEqualTo("Frodo");
 

Specified by:
as in class GenericAssert<Object>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

describedAs

public ObjectAssert describedAs(String description)
Alias for GenericAssert.as(String), since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

For example:

 assertThat(val).describedAs("name").isEqualTo("Frodo");
 

Specified by:
describedAs in class GenericAssert<Object>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

as

public ObjectAssert as(Description description)
Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

For example:

 assertThat(val).as(new BasicDescription("name")).isEqualTo("Frodo");
 

Specified by:
as in class GenericAssert<Object>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

describedAs

public ObjectAssert describedAs(Description description)
Alias for GenericAssert.as(Description), since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

For example:

 assertThat(val).describedAs(new BasicDescription("name")).isEqualTo("Frodo");
 

Specified by:
describedAs in class GenericAssert<Object>
Parameters:
description - the description of the actual value.
Returns:
this assertion object.

satisfies

public ObjectAssert satisfies(Condition<Object> condition)
Verifies that the actual Object satisfies the given condition.

Specified by:
satisfies in class GenericAssert<Object>
Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual Object does not satisfy the given condition.
See Also:
is(Condition)

doesNotSatisfy

public ObjectAssert doesNotSatisfy(Condition<Object> condition)
Verifies that the actual Object does not satisfy the given condition.

Specified by:
doesNotSatisfy in class GenericAssert<Object>
Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual Object satisfies the given condition.
See Also:
isNot(Condition)

is

public ObjectAssert is(Condition<Object> condition)
Alias for satisfies(Condition).

Specified by:
is in class GenericAssert<Object>
Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual Object does not satisfy the given condition.
Since:
1.2

isNot

public ObjectAssert isNot(Condition<Object> condition)
Alias for doesNotSatisfy(Condition).

Specified by:
isNot in class GenericAssert<Object>
Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual Object satisfies the given condition.
Since:
1.2

isNotNull

public ObjectAssert isNotNull()
Verifies that the actual Object is not null.

Specified by:
isNotNull in class GenericAssert<Object>
Returns:
this assertion object.
Throws:
AssertionError - if the actual Object is null.

isSameAs

public ObjectAssert isSameAs(Object expected)
Verifies that the actual Object is the same as the given one.

Specified by:
isSameAs in class GenericAssert<Object>
Parameters:
expected - the given Object to compare the actual Object to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual Object is not the same as the given one.

isNotSameAs

public ObjectAssert isNotSameAs(Object other)
Verifies that the actual Object is not the same as the given one.

Specified by:
isNotSameAs in class GenericAssert<Object>
Parameters:
other - the given Object to compare the actual Object to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual Object is the same as the given one.

isEqualTo

public ObjectAssert isEqualTo(Object expected)
Verifies that the actual Object is equal to the given one.

Specified by:
isEqualTo in class GenericAssert<Object>
Parameters:
expected - the given Object to compare the actual Object to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual Object is not equal to the given one.

isNotEqualTo

public ObjectAssert isNotEqualTo(Object other)
Verifies that the actual Object is not equal to the given one.

Specified by:
isNotEqualTo in class GenericAssert<Object>
Parameters:
other - the given Object to compare the actual Object to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual Object is equal to the given one.

overridingErrorMessage

public ObjectAssert overridingErrorMessage(String message)
Replaces the default message displayed in case of a failure with the given one.

For example, the following assertion:

 assertThat("Hello").isEqualTo("Bye");
 
will fail with the default message "expected:<'[Bye]'> but was:<'[Hello]'>."

We can replace this message with our own:

 assertThat("Hello").overridingErrorMessage("'Hello' should be equal to 'Bye'").isEqualTo("Bye");
 
in this case, the assertion will fail showing the message "'Hello' should be equal to 'Bye'".

Specified by:
overridingErrorMessage in class GenericAssert<Object>
Parameters:
message - the given error message, which will replace the default one.
Returns:
this assertion.


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