Class MapSubject


  • public class MapSubject
    extends Subject
    Propositions for Map subjects.
    • Field Detail

      • actual

        private final java.util.Map<?,​?> actual
      • VALUE_DIFFERENCE_FORMAT

        private static final com.google.common.base.Function<MapSubject.ValueDifference<java.lang.Object,​java.lang.Object>,​java.lang.String> VALUE_DIFFERENCE_FORMAT
        A formatting function for value differences when compared for equality.
      • IN_ORDER

        private static final Ordered IN_ORDER
        Ordered implementation that does nothing because it's already known to be true.
      • ALREADY_FAILED

        private static final Ordered ALREADY_FAILED
        Ordered implementation that does nothing because an earlier check already caused a failure.
    • Constructor Detail

      • MapSubject

        protected MapSubject​(FailureMetadata metadata,
                             java.util.Map<?,​?> map)
        Constructor for use by subclasses. If you want to create an instance of this class itself, call check(...).that(actual).
    • Method Detail

      • isEqualTo

        public final void isEqualTo​(java.lang.Object other)
        Description copied from class: Subject
        Fails if the subject is not equal to the given object. For the purposes of this comparison, two objects are equal if any of the following is true:
        • they are equal according to Objects.equal(java.lang.Object, java.lang.Object)
        • they are arrays and are considered equal by the appropriate Arrays.equals(long[], long[]) overload
        • they are boxed integer types (Byte, Short, Character, Integer, or Long) and they are numerically equal when converted to Long.
        • the actual value is a boxed floating-point type (Double or Float), the expected value is an Integer, and the two are numerically equal when converted to Double. (This allows assertThat(someDouble).isEqualTo(0) to pass.)

        Note: This method does not test the Object.equals(java.lang.Object) implementation itself; it assumes that method is functioning correctly according to its contract. Testing an equals implementation requires a utility such as guava-testlib's EqualsTester.

        In some cases, this method might not even call equals. It may instead perform other tests that will return the same result as long as equals is implemented according to the contract for its type.

        Overrides:
        isEqualTo in class Subject
      • isEmpty

        public final void isEmpty()
        Fails if the map is not empty.
      • isNotEmpty

        public final void isNotEmpty()
        Fails if the map is empty.
      • hasSize

        public final void hasSize​(int expectedSize)
        Fails if the map does not have the given size.
      • containsKey

        public final void containsKey​(java.lang.Object key)
        Fails if the map does not contain the given key.
      • doesNotContainKey

        public final void doesNotContainKey​(java.lang.Object key)
        Fails if the map contains the given key.
      • containsEntry

        public final void containsEntry​(java.lang.Object key,
                                        java.lang.Object value)
        Fails if the map does not contain the given entry.
      • doesNotContainEntry

        public final void doesNotContainEntry​(java.lang.Object key,
                                              java.lang.Object value)
        Fails if the map contains the given entry.
      • containsExactly

        public final Ordered containsExactly()
        Fails if the map is not empty.
      • containsExactly

        public final Ordered containsExactly​(java.lang.Object k0,
                                             java.lang.Object v0,
                                             java.lang.Object... rest)
        Fails if the map does not contain exactly the given set of key/value pairs.

        Warning: the use of varargs means that we cannot guarantee an equal number of key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!

        The arguments must not contain duplicate keys.

      • containsAtLeast

        public final Ordered containsAtLeast​(java.lang.Object k0,
                                             java.lang.Object v0,
                                             java.lang.Object... rest)
      • accumulateMap

        private static java.util.Map<java.lang.Object,​java.lang.Object> accumulateMap​(java.lang.String functionName,
                                                                                            java.lang.Object k0,
                                                                                            java.lang.Object v0,
                                                                                            java.lang.Object... rest)
      • containsExactlyEntriesIn

        public final Ordered containsExactlyEntriesIn​(java.util.Map<?,​?> expectedMap)
        Fails if the map does not contain exactly the given set of entries in the given map.
      • containsAtLeastEntriesIn

        public final Ordered containsAtLeastEntriesIn​(java.util.Map<?,​?> expectedMap)
        Fails if the map does not contain at least the given set of entries in the given map.
      • containsEntriesInAnyOrder

        private boolean containsEntriesInAnyOrder​(java.util.Map<?,​?> expectedMap,
                                                  java.lang.String failVerb,
                                                  boolean allowUnexpected)
      • addKeyTypes

        private static final java.util.Map<java.lang.Object,​java.lang.Object> addKeyTypes​(java.util.Map<?,​?> in)
      • comparingValuesUsing

        public final <A,​E> MapSubject.UsingCorrespondence<A,​E> comparingValuesUsing​(Correspondence<? super A,​? super E> correspondence)
        Starts a method chain for a check in which the actual values (i.e. the values of the Map under test) are compared to expected values using the given Correspondence. The actual values must be of type A, the expected values must be of type E. The check is actually executed by continuing the method chain. For example:
        
         assertThat(actualMap)
           .comparingValuesUsing(correspondence)
           .containsEntry(expectedKey, expectedValue);
         
        where actualMap is a Map<?, A> (or, more generally, a Map<?, ? extends A>), correspondence is a Correspondence<A, E>, and expectedValue is an E.

        Note that keys will always be compared with regular object equality (Object.equals(java.lang.Object)).

        Any of the methods on the returned object may throw ClassCastException if they encounter an actual value that is not of type A or an expected value that is not of type E.