Class ObjectArrayAssert


public class ObjectArrayAssert extends ObjectGroupAssert<Object[]>
Understands assertions for Object arrays. To create a new instance of this class use the method Assertions.assertThat(Object[]).
  • Constructor Details

    • ObjectArrayAssert

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

    • as

      public ObjectArrayAssert 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<Object[]>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public ObjectArrayAssert 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<Object[]>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • as

      public ObjectArrayAssert 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<Object[]>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public ObjectArrayAssert 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<Object[]>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • hasAllElementsOfType

      public ObjectArrayAssert hasAllElementsOfType(Class<?> type)
      Verifies that all the elements in the actual Object array belong to the specified type. Matching includes subclasses of the given type.

      For example, consider the following code listing:

       Number[] numbers = { 2, 6 ,8 };
       assertThat(numbers).hasComponentType(Integer.class);
       
      The assertion hasAllElementsOfType will be successful.

      Parameters:
      type - the expected type.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given type is null.
      AssertionError - if the component type of the actual Object array is not the same as the specified one.
    • hasAtLeastOneElementOfType

      public ObjectArrayAssert hasAtLeastOneElementOfType(Class<?> type)
      Verifies that at least one element in the actual Object array belong to the specified type. Matching includes subclasses of the given type.
      Parameters:
      type - the expected type.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Object does not have any elements of the given type.
    • validateIsNotNull

      private void validateIsNotNull(Class<?> type)
    • contains

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

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

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

      public ObjectArrayAssert doesNotHaveDuplicates()
      Verifies that the actual Object array does not have duplicates.
      Specified by:
      doesNotHaveDuplicates in class ObjectGroupAssert<Object[]>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Object array is null.
      AssertionError - if the actual Object array has duplicates.
    • satisfies

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

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

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

      public ObjectArrayAssert isNot(Condition<Object[]> condition)
      Specified by:
      isNot in class GenericAssert<Object[]>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual Object array satisfies the given condition.
      Since:
      1.2
    • isNotNull

      public ObjectArrayAssert isNotNull()
      Verifies that the actual Object array is not null.
      Specified by:
      isNotNull in class GenericAssert<Object[]>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Object array is null.
    • isNotEmpty

      public ObjectArrayAssert isNotEmpty()
      Verifies that the actual Object array contains at least on element.
      Specified by:
      isNotEmpty in class GroupAssert<Object[]>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Object array is null.
      AssertionError - if the actual Object array is empty.
    • isEqualTo

      public ObjectArrayAssert isEqualTo(Object[] expected)
      Verifies that the actual Object array is equal to the given array. Array equality is checked by Arrays.deepEquals(Object[], Object[]).
      Specified by:
      isEqualTo in class GenericAssert<Object[]>
      Parameters:
      expected - the given array to compare the actual array to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Object array is not equal to the given one.
    • isNotEqualTo

      public ObjectArrayAssert isNotEqualTo(Object[] array)
      Verifies that the actual Object array is not equal to the given array. Array equality is checked by Arrays.deepEquals(Object[], Object[]).
      Specified by:
      isNotEqualTo in class GenericAssert<Object[]>
      Parameters:
      array - the given array to compare the actual array to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Object array is equal to the given one.
    • hasSize

      public ObjectArrayAssert hasSize(int expected)
      Verifies that the number of elements in the actual Object array is equal to the given one.
      Specified by:
      hasSize in class GroupAssert<Object[]>
      Parameters:
      expected - the expected number of elements in the actual Object array.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Object array is null.
      AssertionError - if the number of elements in the actual Object array is not equal to the given one.
    • isSameAs

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

      public ObjectArrayAssert isNotSameAs(Object[] expected)
      Verifies that the actual Object array is not the same as the given array.
      Specified by:
      isNotSameAs in class GenericAssert<Object[]>
      Parameters:
      expected - the given array to compare the actual array to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Object array is the same as the given one.
    • overridingErrorMessage

      public ObjectArrayAssert 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<Object[]>
      Parameters:
      message - the given error message, which will replace the default one.
      Returns:
      this assertion.
    • onProperty

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

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

      protected int actualGroupSize()
      Returns the size of the actual group of values (array, collection, etc.)
      Specified by:
      actualGroupSize in class GroupAssert<Object[]>
      Returns:
      the size of the actual group of values.