Class GroupAssert<T>

Type Parameters:
T - the type of object implementations of this template can verify.
Direct Known Subclasses:
ItemGroupAssert, MapAssert, StringAssert

public abstract class GroupAssert<T> extends GenericAssert<T>
Understands a template for assertion methods related to classes representing groups of values.
  • Constructor Details

    • GroupAssert

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

    • isNullOrEmpty

      public void isNullOrEmpty()
      Verifies that the actual group of values is null or empty.
      Throws:
      AssertionError - if the actual group of values is not null or not empty.
    • isEmpty

      public void isEmpty()
      Verifies that the actual group of values is empty.
      Throws:
      AssertionError - if the actual group of values is null or not empty.
    • hasElements

      private boolean hasElements()
    • isNotEmpty

      protected abstract GroupAssert<T> isNotEmpty()
      Verifies that the actual group contains at least on value.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual group is null or empty.
    • assertIsNotEmpty

      protected final void assertIsNotEmpty()
      Verifies that the actual group of values contains at least one element.
      Throws:
      AssertionError - if the actual group of values is null.
      AssertionError - if the actual group of values is empty.
    • hasSize

      protected abstract GroupAssert<T> hasSize(int expected)
      Verifies that the number of values in the actual group is equal to the given one.
      Parameters:
      expected - the expected number of values in the actual group.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the number of values of the actual group is not equal to the given one.
    • assertHasSize

      protected final void assertHasSize(int expected)
      Verifies that the number of elements in the actual group of values is equal to the given one.
      Parameters:
      expected - the expected number of elements in the actual group of values.
      Throws:
      AssertionError - if the actual group of values is null.
      AssertionError - if the number of elements of the actual group of values is not equal to the given one.
    • actualGroupSize

      protected abstract int actualGroupSize()
      Returns the size of the actual group of values (array, collection, etc.)
      Returns:
      the size of the actual group of values.
    • as

      protected abstract GroupAssert<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");
       

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

      protected abstract GroupAssert<T> 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<T>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • as

      protected abstract GroupAssert<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");
       

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

      protected abstract GroupAssert<T> 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<T>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • overridingErrorMessage

      protected abstract GroupAssert<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'".

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