Class MapSubject
- Direct Known Subclasses:
MapWithProtoValuesSubject
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A partially specified check in which the actual values (i.e. the values of theMap
under test) are compared to expected values using aCorrespondence
.Nested classes/interfaces inherited from class com.google.common.truth.Subject
Subject.Factory<SubjectT extends Subject, ActualT>
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
MapSubject
(FailureMetadata metadata, @Nullable Map<?, ?> map) Constructor for use by subclasses. -
Method Summary
Modifier and TypeMethodDescriptionfinal <A extends @Nullable Object, E extends @Nullable Object>
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 theMap
under test) are compared to expected values using the givenCorrespondence
.final Ordered
final Ordered
containsAtLeastEntriesIn
(Map<?, ?> expectedMap) Checks that the actual map contains at least the given set of entries in the given map.final void
containsEntry
(@Nullable Object key, @Nullable Object value) Checks that the actual map contains the given entry.final Ordered
Checks that the actual map is empty.final Ordered
Checks that the actual map contains exactly the given set of key/value pairs.final Ordered
containsExactlyEntriesIn
(Map<?, ?> expectedMap) Checks that the actual map contains exactly the given set of entries in the given map.final void
containsKey
(@Nullable Object key) Checks that the actual map contains the given key.final void
doesNotContainEntry
(@Nullable Object key, @Nullable Object value) Checks that the actual map does not contain the given entry.final void
Checks that the actual map does not contain the given key.final <V extends @Nullable Object>
MapSubject.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 givenCorrespondence.DiffFormatter
to describe the difference between an actual map (i.e. a value in theMap
under test) and the value it is expected to be equal to, but isn't.final void
hasSize
(int expectedSize) Checks that the actual map has the given size.final void
isEmpty()
Checks that the actual map is empty.final void
Checks that the value under test is equal to the given object.final void
Checks that the actual map is not empty.Methods inherited from class com.google.common.truth.Subject
actualCustomStringRepresentation, check, equals, failWithActual, failWithActual, failWithoutActual, hashCode, ignoreCheck, isAnyOf, isIn, isInstanceOf, isNoneOf, isNotEqualTo, isNotIn, isNotInstanceOf, isNotNull, isNotSameInstanceAs, isNull, isSameInstanceAs, toString
-
Constructor Details
-
MapSubject
Constructor for use by subclasses. If you want to create an instance of this class itself, callcheck(...)
.that(actual)
.
-
-
Method Details
-
isEqualTo
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:- they are equal according to
Objects.equals(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
, orLong
) and they are numerically equal when converted toLong
. - the actual value is a boxed floating-point type (
Double
orFloat
), the expected value is anInteger
, and the two are numerically equal when converted toDouble
. (This allowsassertThat(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 anequals
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 asequals
is implemented according to the contract for its type. - they are equal according to
-
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 expectedSize) Checks that the actual map has the given size. -
containsKey
-
doesNotContainKey
-
containsEntry
-
doesNotContainEntry
-
containsExactly
Checks that the actual map is empty. -
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.
-
containsAtLeast
-
containsExactlyEntriesIn
Checks that the actual map contains exactly the given set of entries in the given map. -
containsAtLeastEntriesIn
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 Object> 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 theMap
under test) are compared to expected values using the givenCorrespondence
. The actual values must be of typeA
, the expected values must be of typeE
. The check is actually executed by continuing the method chain. For example:
whereassertThat(actualMap) .comparingValuesUsing(correspondence) .containsEntry(expectedKey, expectedValue);
actualMap
is aMap<?, A>
(or, more generally, aMap<?, ? extends A>
),correspondence
is aCorrespondence<A, E>
, andexpectedValue
is anE
.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 typeA
or an expected value that is not of typeE
. -
formattingDiffsUsing
public final <V extends @Nullable Object> MapSubject.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 givenCorrespondence.DiffFormatter
to describe the difference between an actual map (i.e. a value in theMap
under test) and the value it is expected to be equal to, but isn't. The actual and expected values must be of typeV
. The check is actually executed by continuing the method chain. For example:
whereassertThat(actualMap) .formattingDiffsUsing(FooTestHelper::formatDiff) .containsExactly(key1, foo1, key2, foo2, key3, foo3);
actualMap
is aMap<?, Foo>
(or, more generally, aMap<?, ? extends Foo>
),FooTestHelper.formatDiff
is a static method taking twoFoo
arguments and returning aString
, andfoo1
,foo2
, andfoo3
areFoo
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 typeV
.- Since:
- 1.1
-