Class MapAssert


public class MapAssert extends GroupAssert<Map<?,?>>
Understands assertions for Maps. To create a new instance of this class use the method Assertions.assertThat(Map).
  • Field Details

  • Constructor Details

    • MapAssert

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

    • as

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

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

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

      public MapAssert 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 GroupAssert<Map<?,?>>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • includes

      public MapAssert includes(MapAssert.Entry... entries)
      Verifies that the actual Map contains the given entries.

      Example:

       // static import org.fest.assertions.Assertions.*;
       // static import org.fest.assertions.MapAssert.*;
      
       assertThat(myMap).includes(entry("jedi", yoda), entry("sith", anakin));
       

      Parameters:
      entries - the given entries.
      Returns:
      this assertion error.
      Throws:
      AssertionError - if the actual map is null.
      AssertionError - if the actual Map does not contain any of the given entries.
      NullPointerException - if the given array of entries is null.
      NullPointerException - if any of the entries in the given array is null.
    • excludes

      public MapAssert excludes(MapAssert.Entry... entries)
      Verifies that the actual Map does not contain the given entries.

      Example:

       // static import org.fest.assertions.Assertions.*;
       // static import org.fest.assertions.MapAssert.*;
      
       assertThat(myMap).excludes(entry("jedi", yoda), entry("sith", anakin));
       

      Parameters:
      entries - the given entries.
      Returns:
      this assertion error.
      Throws:
      AssertionError - if the actual map is null.
      AssertionError - if the actual Map contains any of the given entries.
      NullPointerException - if the given array of entries is null.
      NullPointerException - if any of the entries in the given array is null.
    • containsEntry

      private boolean containsEntry(MapAssert.Entry e)
    • entryOrEntries

      private String entryOrEntries(List<MapAssert.Entry> found)
    • entry

      public static MapAssert.Entry entry(Object key, Object value)
      Creates a new map entry.
      Parameters:
      key - the key of the entry.
      value - the value of the entry.
      Returns:
      the created entry.
      See Also:
    • failIfNotFound

      private void failIfNotFound(String description, Collection<?> notFound)
    • validate

      private void validate(String description, Object[] objects)
    • failIfFound

      private void failIfFound(String description, Collection<?> found)
    • hasSize

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

      private String formattedActual()
    • isEqualTo

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

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

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

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

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

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

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

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

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

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

      protected int actualGroupSize()
      Returns the number of elements in the actual Map.
      Specified by:
      actualGroupSize in class GroupAssert<Map<?,?>>
      Returns:
      the number of elements in the actual Map.
    • overridingErrorMessage

      public MapAssert 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 GroupAssert<Map<?,?>>
      Parameters:
      message - the given error message, which will replace the default one.
      Returns:
      this assertion.