Class GenericAssert<T>

java.lang.Object
org.fest.assertions.Assert
org.fest.assertions.GenericAssert<T>
Type Parameters:
T - the type of object implementations of this template can verify.
Direct Known Subclasses:
BooleanAssert, ByteAssert, CharAssert, ComparableAssert, DoubleAssert, FileAssert, FloatAssert, GroupAssert, ImageAssert, IntAssert, LongAssert, ObjectAssert, ShortAssert, ThrowableAssert

public abstract class GenericAssert<T> extends Assert
Understands a template for assertion methods.
  • Field Details

    • actual

      protected final T actual
  • Constructor Details

    • GenericAssert

      protected GenericAssert(T actual)
      Creates a new GenericAssert.
      Parameters:
      actual - the actual target to verify.
  • Method Details

    • 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

      protected abstract GenericAssert<T> satisfies(Condition<T> 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:
    • doesNotSatisfy

      protected abstract GenericAssert<T> doesNotSatisfy(Condition<T> 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:
    • is

      protected abstract GenericAssert<T> is(Condition<T> 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

      protected abstract GenericAssert<T> isNot(Condition<T> 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

      protected abstract GenericAssert<T> 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

      protected abstract GenericAssert<T> 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

      protected abstract GenericAssert<T> 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

      protected abstract GenericAssert<T> 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

      protected abstract GenericAssert<T> isEqualTo(T 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

      protected abstract GenericAssert<T> isNotEqualTo(T 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

      protected abstract GenericAssert<T> isNotNull()
      Verifies that the actual value is not null.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
    • isSameAs

      protected abstract GenericAssert<T> isSameAs(T 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

      protected abstract GenericAssert<T> isNotSameAs(T 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.
    • assertSatisfies

      protected final void assertSatisfies(Condition<T> condition)
      Verifies that the actual value satisfies the given condition.
      Parameters:
      condition - the condition to check.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual value does not satisfy the given condition.
    • errorMessageIfConditionNotSatisfied

      private String errorMessageIfConditionNotSatisfied(Condition<T> condition)
    • assertIs

      protected final void assertIs(Condition<T> condition)
      Verifies that the actual value satisfies the given condition.
      Parameters:
      condition - the condition to check.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual value does not satisfy the given condition.
    • errorMessageIfIsNot

      private String errorMessageIfIsNot(Condition<T> condition)
    • assertDoesNotSatisfy

      protected final void assertDoesNotSatisfy(Condition<T> condition)
      Verifies that the actual value does not satisfy the given condition.
      Parameters:
      condition - the condition to check.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual value satisfies the given condition.
    • errorMessageIfConditionSatisfied

      private String errorMessageIfConditionSatisfied(Condition<T> condition)
    • assertIsNot

      protected final void assertIsNot(Condition<T> condition)
      Verifies that the actual value does not satisfy the given condition.
      Parameters:
      condition - the condition to check.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual value satisfies the given condition.
    • matches

      private boolean matches(Condition<T> condition)
    • validateIsNotNull

      private void validateIsNotNull(Condition<T> condition)
    • errorMessageIfIs

      private String errorMessageIfIs(Condition<T> condition)
    • assertEqualTo

      protected final void assertEqualTo(T expected)
      Verifies that the actual value is equal to the given one.
      Parameters:
      expected - the value to compare the actual value to.
      Throws:
      AssertionError - if the actual value is not equal to the given one.
    • assertNotEqualTo

      protected final void assertNotEqualTo(T other)
      Verifies that the actual value is not equal to the given one.
      Parameters:
      other - the value to compare the actual value to.
      Throws:
      AssertionError - if the actual value is equal to the given one.
    • assertNotNull

      protected final void assertNotNull()
      Verifies that the actual value is not null.
      Throws:
      AssertionError - if the actual value is null.
    • assertSameAs

      protected final void assertSameAs(T expected)
      Verifies that the actual value is the same as the given one.
      Parameters:
      expected - the value to compare the actual value to.
      Throws:
      AssertionError - if the actual value is not the same as the given one.
    • assertNotSameAs

      protected final void assertNotSameAs(T expected)
      Verifies that the actual value is not the same as the given one.
      Parameters:
      expected - the value to compare the actual value to.
      Throws:
      AssertionError - if the actual value is the same as the given one.
    • overridingErrorMessage

      protected abstract GenericAssert<T> 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