Class CollectionAssert


public class CollectionAssert extends ObjectGroupAssert<Collection<?>>
Understands assertions for collections. To create a new instance of this class use the method Assertions.assertThat(Collection).
  • Constructor Details

    • CollectionAssert

      protected CollectionAssert(Collection<?> actual)
      Creates a new CollectionAssert.
      Parameters:
      actual - the target to verify.
  • Method Details

    • contains

      public CollectionAssert contains(Object... objects)
      Verifies that the actual collection contains the given objects.
      Specified by:
      contains in class ObjectGroupAssert<Collection<?>>
      Parameters:
      objects - the objects to look for.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is null.
      NullPointerException - if the given array is null.
      AssertionError - if the actual collection does not contain the given objects.
    • containsOnly

      public CollectionAssert containsOnly(Object... objects)
      Verifies that the actual collection contains the given objects only.
      Specified by:
      containsOnly in class ObjectGroupAssert<Collection<?>>
      Parameters:
      objects - the objects to look for.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is null.
      NullPointerException - if the given array is null.
      AssertionError - if the actual collection does not contain the given objects, or if the actual collection contains elements other than the ones specified.
    • excludes

      public CollectionAssert excludes(Object... objects)
      Verifies that the actual collection does not contain the given objects.
      Specified by:
      excludes in class ObjectGroupAssert<Collection<?>>
      Parameters:
      objects - the objects that the collection should exclude.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is null.
      NullPointerException - if the given array is null.
      AssertionError - if the actual collection contains any of the given objects.
    • doesNotHaveDuplicates

      public CollectionAssert doesNotHaveDuplicates()
      Verifies that the actual collection does not have duplicates.
      Specified by:
      doesNotHaveDuplicates in class ObjectGroupAssert<Collection<?>>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is null.
      AssertionError - if the actual collection has duplicates.
    • as

      public CollectionAssert 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 ObjectGroupAssert<Collection<?>>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public CollectionAssert 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 ObjectGroupAssert<Collection<?>>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • as

      public CollectionAssert 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 ObjectGroupAssert<Collection<?>>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public CollectionAssert 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 ObjectGroupAssert<Collection<?>>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • satisfies

      public CollectionAssert satisfies(Condition<Collection<?>> condition)
      Verifies that the actual collection satisfies the given condition.
      Specified by:
      satisfies in class GenericAssert<Collection<?>>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual collection does not satisfy the given condition.
      See Also:
    • doesNotSatisfy

      public CollectionAssert doesNotSatisfy(Condition<Collection<?>> condition)
      Verifies that the actual collection does not satisfy the given condition.
      Specified by:
      doesNotSatisfy in class GenericAssert<Collection<?>>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual collection satisfies the given condition.
      See Also:
    • is

      public CollectionAssert is(Condition<Collection<?>> condition)
      Specified by:
      is in class GenericAssert<Collection<?>>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual collection does not satisfy the given condition.
      Since:
      1.2
    • isNot

      public CollectionAssert isNot(Condition<Collection<?>> condition)
      Specified by:
      isNot in class GenericAssert<Collection<?>>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual collection satisfies the given condition.
      Since:
      1.2
    • isNotNull

      public CollectionAssert isNotNull()
      Verifies that the actual collection is not null.
      Specified by:
      isNotNull in class GenericAssert<Collection<?>>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is null.
    • isNotEmpty

      public CollectionAssert isNotEmpty()
      Verifies that the actual collection contains at least on element.
      Specified by:
      isNotEmpty in class GroupAssert<Collection<?>>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is null.
      AssertionError - if the actual collection is empty.
    • hasSize

      public CollectionAssert hasSize(int expected)
      Verifies that the number of elements in the actual collection is equal to the given one.
      Specified by:
      hasSize in class GroupAssert<Collection<?>>
      Parameters:
      expected - the expected number of elements in the actual collection.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is null.
      AssertionError - if the number of elements of the actual collection is not equal to the given one.
    • actualGroupSize

      protected int actualGroupSize()
      Returns the number of elements in the actual collection.
      Specified by:
      actualGroupSize in class GroupAssert<Collection<?>>
      Returns:
      the number of elements in the actual collection.
      Throws:
      AssertionError - if the actual collection is null.
    • isEqualTo

      public CollectionAssert isEqualTo(Collection<?> expected)
      Verifies that the actual collection is equal to the given one.
      Specified by:
      isEqualTo in class GenericAssert<Collection<?>>
      Parameters:
      expected - the given collection to compare the actual collection to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is not equal to the given one.
    • isNotEqualTo

      public CollectionAssert isNotEqualTo(Collection<?> other)
      Verifies that the actual collection is not equal to the given one.
      Specified by:
      isNotEqualTo in class GenericAssert<Collection<?>>
      Parameters:
      other - the given collection to compare the actual collection to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is equal to the given one.
    • isSameAs

      public CollectionAssert isSameAs(Collection<?> expected)
      Verifies that the actual collection is the same as the given one.
      Specified by:
      isSameAs in class GenericAssert<Collection<?>>
      Parameters:
      expected - the given collection to compare the actual collection to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is not the same as the given one.
    • isNotSameAs

      public CollectionAssert isNotSameAs(Collection<?> other)
      Verifies that the actual collection is not the same as the given one.
      Specified by:
      isNotSameAs in class GenericAssert<Collection<?>>
      Parameters:
      other - the given collection to compare the actual collection to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual collection is the same as the given one.
    • overridingErrorMessage

      public CollectionAssert 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 ObjectGroupAssert<Collection<?>>
      Parameters:
      message - the given error message, which will replace the default one.
      Returns:
      this assertion.
    • onProperty

      public CollectionAssert onProperty(String propertyName)
      Creates a new instance of CollectionAssert whose target collection contains the values of the given property name from the elements of this CollectionAssert's collection. Property access works with both simple properties like Person.age and nested properties Person.father.age.

      For example, let's say we have a collection of Person objects and you want to verify their age:

       assertThat(persons).onProperty("age").containsOnly(25, 16, 44, 37); // simple property
       assertThat(persons).onProperty("father.age").containsOnly(55, 46, 74, 62); // nested property
       

      Specified by:
      onProperty in class ObjectGroupAssert<Collection<?>>
      Parameters:
      propertyName - the name of the property to extract values from the actual collection to build a new CollectionAssert.
      Returns:
      a new CollectionAssert containing the values of the given property name from the elements of this CollectionAssert's collection.
      Throws:
      AssertionError - if the actual collection is null.
      org.fest.util.IntrospectionError - if an element in the given collection does not have a matching property.
      Since:
      1.3
    • actualAsSet

      protected Set<Object> actualAsSet()
      Returns the actual value as a Set.
      Specified by:
      actualAsSet in class ItemGroupAssert<Collection<?>>
      Returns:
      the actual value as a Set.
    • actualAsList

      protected List<Object> actualAsList()
      Returns the actual value as a List.
      Specified by:
      actualAsList in class ItemGroupAssert<Collection<?>>
      Returns:
      the actual value as a List.