Class MapSubject
- java.lang.Object
-
- com.google.common.truth.Subject
-
- com.google.common.truth.MapSubject
-
- Direct Known Subclasses:
MapWithProtoValuesSubject
public class MapSubject extends Subject
Propositions forMapsubjects.- Author:
- Christian Gruber, Kurt Alfred Kluever
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classMapSubject.UsingCorrespondence<A,E>A partially specified check in which the actual values (i.e.-
Nested classes/interfaces inherited from class com.google.common.truth.Subject
Subject.Factory<SubjectT extends Subject,ActualT>
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedMapSubject(FailureMetadata metadata, @Nullable Map<?,?> map)Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <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.OrderedcontainsAtLeast(@Nullable Object k0, @Nullable Object v0, Object... rest)OrderedcontainsAtLeastEntriesIn(Map<?,?> expectedMap)Fails if the map does not contain at least the given set of entries in the given map.voidcontainsEntry(@Nullable Object key, @Nullable Object value)Fails if the map does not contain the given entry.OrderedcontainsExactly()Fails if the map is not empty.OrderedcontainsExactly(@Nullable Object k0, @Nullable Object v0, Object... rest)Fails if the map does not contain exactly the given set of key/value pairs.OrderedcontainsExactlyEntriesIn(Map<?,?> expectedMap)Fails if the map does not contain exactly the given set of entries in the given map.voidcontainsKey(@Nullable Object key)Fails if the map does not contain the given key.voiddoesNotContainEntry(@Nullable Object key, @Nullable Object value)Fails if the map contains the given entry.voiddoesNotContainKey(@Nullable Object key)Fails if the map contains the given key.<V> 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.DiffFormatterto describe the difference between an actual value (i.e.voidhasSize(int expectedSize)Fails if the map does not have the given size.voidisEmpty()Fails if the map is not empty.voidisEqualTo(@Nullable Object other)Fails if the subject is not equal to the given object.voidisNotEmpty()Fails if the map is 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 Detail
-
MapSubject
protected MapSubject(FailureMetadata metadata, @Nullable Map<?,?> map)
Constructor for use by subclasses. If you want to create an instance of this class itself, callcheck(...).that(actual).
-
-
Method Detail
-
isEqualTo
public final void isEqualTo(@Nullable Object other)
Description copied from class:SubjectFails 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, orLong) and they are numerically equal when converted toLong. - the actual value is a boxed floating-point type (
DoubleorFloat), 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 anequalsimplementation 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 asequalsis implemented according to the contract for its type. - they are equal according to
-
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(@Nullable Object key)
Fails if the map does not contain the given key.
-
doesNotContainKey
public final void doesNotContainKey(@Nullable Object key)
Fails if the map contains the given key.
-
containsEntry
public final void containsEntry(@Nullable Object key, @Nullable Object value)
Fails if the map does not contain the given entry.
-
doesNotContainEntry
public final void doesNotContainEntry(@Nullable Object key, @Nullable Object value)
Fails if the map contains the given entry.
-
containsExactly
@CanIgnoreReturnValue public final Ordered containsExactly()
Fails if the map is not empty.
-
containsExactly
@CanIgnoreReturnValue public final Ordered containsExactly(@Nullable Object k0, @Nullable Object v0, 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
@CanIgnoreReturnValue public final Ordered containsAtLeast(@Nullable Object k0, @Nullable Object v0, Object... rest)
-
containsExactlyEntriesIn
@CanIgnoreReturnValue public final Ordered containsExactlyEntriesIn(Map<?,?> expectedMap)
Fails if the map does not contain exactly the given set of entries in the given map.
-
containsAtLeastEntriesIn
@CanIgnoreReturnValue public final Ordered containsAtLeastEntriesIn(Map<?,?> expectedMap)
Fails if the map does not contain at least the given set of entries in the given map.
-
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 theMapunder 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);actualMapis aMap<?, A>(or, more generally, aMap<?, ? extends A>),correspondenceis aCorrespondence<A, E>, andexpectedValueis 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
ClassCastExceptionif they encounter an actual value that is not of typeAor an expected value that is not of typeE.
-
formattingDiffsUsing
public final <V> 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.DiffFormatterto describe the difference between an actual value (i.e. a value in theMapunder 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);actualMapis aMap<?, Foo>(or, more generally, aMap<?, ? extends Foo>),FooTestHelper.formatDiffis a static method taking twoFooarguments and returning aString, andfoo1,foo2, andfoo3areFooinstances.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
ClassCastExceptionif they encounter a value that is not of typeV.- Since:
- 1.1
-
-