Class ListAssert


public class ListAssert extends ObjectGroupAssert<List<?>>
Understands assertions for Lists. To create a new instance of this class use the method Assertions.assertThat(List).
Since:
1.1
  • Constructor Details

    • ListAssert

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

    • contains

      public ListAssert contains(Object o, Index index)
      Verifies that the actual List contains the given object at the given index.
      Parameters:
      o - the object to look for.
      index - the index where the object should be stored in the actual List.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given Index is null.
      IndexOutOfBoundsException - if the value of the given Index is negative, or equal to or greater than the size of the actual List.
      AssertionError - if the given List does not contain the given object at the given index.
    • failElementNotFound

      private void failElementNotFound(Object e, Object a, int index)
    • failIndexOutOfBounds

      private void failIndexOutOfBounds(int index)
    • containsSequence

      public ListAssert containsSequence(Object... sequence)
      Verifies that the actual List contains the given sequence of objects, without any other objects between them.
      Parameters:
      sequence - the sequence of objects to look for.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual List is null.
      AssertionError - if the given array is null.
      AssertionError - if the actual List does not contain the given sequence of objects.
    • failIfSequenceNotFound

      private void failIfSequenceNotFound(Object[] notFound)
    • startsWith

      public ListAssert startsWith(Object... sequence)
      Verifies that the actual List starts with the given sequence of objects, without any other objects between them. Same as containsSequence(java.lang.Object...), but verifies also that first given object is also first element of List.
      Parameters:
      sequence - the sequence of objects to look for.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual List is null.
      AssertionError - if the given array is null.
      AssertionError - if the actual List is not empty and with the given sequence of objects is empty.
      AssertionError - if the actual List does not start with the given sequence of objects.
    • failIfNotStartingWithSequence

      private void failIfNotStartingWithSequence(Object[] notFound)
    • endsWith

      public ListAssert endsWith(Object... sequence)
      Verifies that the actual List ends with the given sequence of objects, without any other objects between them. Same as containsSequence(java.lang.Object...), but verifies also that last given object is also last element of List.
      Parameters:
      sequence - the sequence of objects to look for.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual List is null.
      AssertionError - if the given array is null.
      AssertionError - if the actual List is not empty and with the given sequence of objects is empty.
      AssertionError - if the actual List does not end with the given sequence of objects.
    • failIfNotEndingWithSequence

      private void failIfNotEndingWithSequence(Object[] notFound)
    • contains

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      public ListAssert containsExactly(Object... objects)
      Verifies that the actual List contains the given objects, in the same order. This method works just like isEqualTo(List), with the difference that internally the given array is converted to a List.
      Parameters:
      objects - the objects to look for.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual List is null.
      NullPointerException - if the given array is null.
      AssertionError - if the actual List does not contain the given objects.
    • isEqualTo

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

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

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

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

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

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

      For example, let's say we have a list 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<List<?>>
      Parameters:
      propertyName - the name of the property to extract values from the actual list to build a new ListAssert.
      Returns:
      a new ListAssert containing the values of the given property name from the elements of this ListAssert's list.
      Throws:
      AssertionError - if the actual list is null.
      org.fest.util.IntrospectionError - if an element in the given list 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<List<?>>
      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<List<?>>
      Returns:
      the actual value as a List.