org.fest.assertions
Class GenericAssert<S,A>

java.lang.Object
  extended by org.fest.assertions.Assert
      extended by org.fest.assertions.GenericAssert<S,A>
Type Parameters:
S - used to simulate "self types." For more information please read "Emulating 'self types' using Java Generics to simplify fluent API implementation."
A - the type the "actual" value.
Direct Known Subclasses:
BooleanAssert, ByteAssert, CharAssert, ComparableAssert, DoubleAssert, FileAssert, FloatAssert, GroupAssert, ImageAssert, IntAssert, LongAssert, ObjectAssert, ShortAssert, ThrowableAssert

public abstract class GenericAssert<S,A>
extends Assert

Template for assertions.

Author:
Yvonne Wang, Alex Ruiz

Field Summary
protected  A actual
           
protected  S myself
           
 
Constructor Summary
protected GenericAssert(Class<S> selfType, A actual)
          Creates a new GenericAssert.
 
Method Summary
 S as(Description description)
          Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails.
 S as(String description)
          Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails.
 S describedAs(Description description)
          Alias for as(Description), since "as" is a keyword in Groovy.
 S describedAs(String description)
          Alias for as(String), since "as" is a keyword in Groovy.
 S doesNotSatisfy(Condition<A> condition)
          Verifies that the actual value does not satisfy the given condition.
 S is(Condition<A> condition)
          Alias for satisfies(Condition).
 S isEqualTo(A expected)
          Verifies that the actual value is equal to the given one.
 S isIn(Collection<?> values)
          Verifies that the actual value is in the given collection.
 S isIn(Object... values)
          Verifies that the actual value is in the given values.
 S isNot(Condition<A> condition)
          Alias for doesNotSatisfy(Condition).
 S isNotEqualTo(A other)
          Verifies that the actual value is not equal to the given one.
 S isNotIn(Collection<?> values)
          Verifies that the actual value is in the given collection.
 S isNotIn(Object... values)
          Verifies that the actual value is in the given values.
 S isNotNull()
          Verifies that the actual value is not null.
 S isNotSameAs(A other)
          Verifies that the actual value is not the same as the given one.
 void isNull()
          Asserts that the actual value (specified in the constructor of this class) is null.
 S isSameAs(A expected)
          Verifies that the actual value is the same as the given one.
 S overridingErrorMessage(String message)
          Replaces the default message displayed in case of a failure with the given one.
 S satisfies(Condition<A> condition)
          Verifies that the actual value satisfies the given condition.
 
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
 

Field Detail

actual

protected final A actual

myself

protected final S myself
Constructor Detail

GenericAssert

protected GenericAssert(Class<S> selfType,
                        A actual)
Creates a new GenericAssert.

Parameters:
selfType - the "self type."
actual - the actual value to verify.
Method Detail

isNull

public final void isNull()
Asserts that the actual value (specified in the constructor of this class) is null.

Throws:
AssertionError - if the actual value is not null.

satisfies

public final S satisfies(Condition<A> condition)
Verifies that the actual value satisfies the given condition.

Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual value does not satisfy the given condition.
See Also:
is(Condition)

doesNotSatisfy

public final S doesNotSatisfy(Condition<A> condition)
Verifies that the actual value does not satisfy the given condition.

Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual value satisfies the given condition.
See Also:
isNot(Condition)

is

public final S is(Condition<A> condition)
Alias for satisfies(Condition).

Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual value does not satisfy the given condition.
Since:
1.2

isNot

public final S isNot(Condition<A> condition)
Alias for doesNotSatisfy(Condition).

Parameters:
condition - the given condition.
Returns:
this assertion object.
Throws:
NullPointerException - if the given condition is null.
AssertionError - if the actual value satisfies the given condition.
Since:
1.2

as

public S 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");
 

Parameters:
description - the description of the actual value.
Returns:
this assertion object.

describedAs

public S describedAs(String description)
Alias for 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");
 

Parameters:
description - the description of the actual value.
Returns:
this assertion object.

as

public S 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");
 

Parameters:
description - the description of the actual value.
Returns:
this assertion object.

describedAs

public S describedAs(Description description)
Alias for 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");
 

Parameters:
description - the description of the actual value.
Returns:
this assertion object.

isEqualTo

public S isEqualTo(A expected)
Verifies that the actual value is equal to the given one.

Parameters:
expected - the given value to compare the actual value to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual value is not equal to the given one.

isNotEqualTo

public S isNotEqualTo(A other)
Verifies that the actual value is not equal to the given one.

Parameters:
other - the given value to compare the actual value to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual value is equal to the given one.

isNotNull

public final S isNotNull()
Verifies that the actual value is not null.

Returns:
this assertion object.
Throws:
AssertionError - if the actual value is null.

isSameAs

public final S isSameAs(A expected)
Verifies that the actual value is the same as the given one.

Parameters:
expected - the given value to compare the actual value to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual value is not the same as the given one.

isNotSameAs

public final S isNotSameAs(A other)
Verifies that the actual value is not the same as the given one.

Parameters:
other - the given value to compare the actual value to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual value is the same as the given one.

isIn

public final S isIn(Object... values)
Verifies that the actual value is in the given values.

Parameters:
values - the given values to search the actual value in.
Returns:
this assertion object.
Throws:
AssertionError - if the actual value is not in the given values.
NullPointerException - if the given parameter is null.

isIn

public final S isIn(Collection<?> values)
Verifies that the actual value is in the given collection.

Parameters:
values - the given collection to search the actual value in. must not be null.
Returns:
this assertion object.
Throws:
AssertionError - if the actual value is not in the given collection.
NullPointerException - if the given collection is null.

isNotIn

public final S isNotIn(Object... values)
Verifies that the actual value is in the given values.

Parameters:
values - the given values to search the actual value in.
Returns:
this assertion object.
Throws:
AssertionError - if the actual value is not in the given values.
NullPointerException - if the given parameter is null.

isNotIn

public final S isNotIn(Collection<?> values)
Verifies that the actual value is in the given collection.

Parameters:
values - the given collection to search the actual value in. must not be null.
Returns:
this assertion object.
Throws:
AssertionError - if the actual value is not in the given collection.
NullPointerException - if the given collection is null.

overridingErrorMessage

public S 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'".

Parameters:
message - the given error message, which will replace the default one.
Returns:
this assertion.
Since:
1.2


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