Class MapSubject

    • Constructor Detail

      • MapSubject

        protected MapSubject​(FailureMetadata metadata,
                             @Nullable Map<?,​?> actual)
        The constructor is for use by subclasses only. If you want to create an instance of this class itself, call check(...).that(actual).
    • Method Detail

      • isEqualTo

        public final void isEqualTo​(@Nullable Object expected)
        Description copied from class: Subject
        Checks that the value under test is equal to the given object. For the purposes of this comparison, two objects are equal if any of the following is true:

        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()
        Checks that the actual map is empty.
      • isNotEmpty

        public final void isNotEmpty()
        Checks that the actual map is not empty.
      • hasSize

        public final void hasSize​(int size)
        Checks that the actual map has the given size.
      • containsKey

        public final void containsKey​(@Nullable Object key)
        Checks that the actual map contains the given key.
      • doesNotContainKey

        public final void doesNotContainKey​(@Nullable Object key)
        Checks that the actual map does not contain the given key.
      • containsEntry

        public final void containsEntry​(@Nullable Object key,
                                        @Nullable Object value)
        Checks that the actual map contains the given entry.
      • doesNotContainEntry

        public final void doesNotContainEntry​(@Nullable Object key,
                                              @Nullable Object value)
        Checks that the actual map does not contain the given entry.
      • containsExactly

        @CanIgnoreReturnValue
        public final Ordered containsExactly​(@Nullable Object k0,
                                             @Nullable Object v0,
                                             @Nullable Object... rest)
        Checks that the actual map contains 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.

      • containsExactlyEntriesIn

        @CanIgnoreReturnValue
        public final Ordered containsExactlyEntriesIn​(@Nullable Map<?,​?> expected)
        Checks that the actual map contains exactly the given set of entries in the given map.
      • containsAtLeastEntriesIn

        @CanIgnoreReturnValue
        public final Ordered containsAtLeastEntriesIn​(@Nullable Map<?,​?> expected)
        Checks that the actual map contains at least the given set of entries in the given map.
      • comparingValuesUsing

        public final <A extends @Nullable Object,​E extends @Nullable ObjectMapSubject.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 map that is not of type A or an expected value that is not of type E.

      • formattingDiffsUsing

        public final <V extends @Nullable ObjectMapSubject.UsingCorrespondence<V,​V> formattingDiffsUsing​(Correspondence.DiffFormatter<? super V,​? super V> formatter)
        Starts a method chain for a check in which failure messages may use the given Correspondence.DiffFormatter to describe the difference between an actual map (i.e. a value in the Map under test) and the value it is expected to be equal to, but isn't. The actual and expected values must be of type V. The check is actually executed by continuing the method chain. For example:
        
         assertThat(actualMap)
             .formattingDiffsUsing(FooTestHelper::formatDiff)
             .containsExactly(key1, foo1, key2, foo2, key3, foo3);
         
        where actualMap is a Map<?, Foo> (or, more generally, a Map<?, ? extends Foo>), FooTestHelper.formatDiff is a static method taking two Foo arguments and returning a String, and foo1, foo2, and foo3 are Foo instances.

        Unlike when using comparingValuesUsing(com.google.common.truth.Correspondence<? super A, ? super E>), the values are still compared using object equality, so this method does not affect whether a test passes or fails.

        Any of the methods on the returned object may throw ClassCastException if they encounter a value that is not of type V.

        Since:
        1.1