Class AbstractMapAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>,K,V>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<SELF,ACTUAL>
-
- org.assertj.core.api.AbstractObjectAssert<SELF,ACTUAL>
-
- org.assertj.core.api.AbstractMapAssert<SELF,ACTUAL,K,V>
-
- Type Parameters:
SELF
- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.ACTUAL
- the type of the "actual" value.K
- the type of keys in the map.V
- the type of values in the map.
- All Implemented Interfaces:
Assert<SELF,ACTUAL>
,Descriptable<SELF>
,EnumerableAssert<SELF,java.util.Map.Entry<? extends K,? extends V>>
,ExtensionPoints<SELF,ACTUAL>
- Direct Known Subclasses:
MapAssert
public abstract class AbstractMapAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>,K,V> extends AbstractObjectAssert<SELF,ACTUAL> implements EnumerableAssert<SELF,java.util.Map.Entry<? extends K,? extends V>>
Base class for all implementations of assertions forMap
s.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) Maps
maps
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, customRepresentation, info, myself, objects, printAssertionsDescription, throwUnsupportedExceptionOnEquals
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractMapAssert(ACTUAL actual, java.lang.Class<?> selfType)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description SELF
allSatisfy(java.util.function.BiConsumer<? super K,? super V> entryRequirements)
Verifies that all the actual map entries satisfy the givenentryRequirements
.SELF
anySatisfy(java.util.function.BiConsumer<? super K,? super V> entryRequirements)
Verifies that at least one map entry satisfies the givenentryRequirements
.SELF
as(java.lang.String description, java.lang.Object... args)
Sets the description of the assertion that is going to be called after.SELF
as(Description description)
Sets the description of the assertion that is going to be called after.private void
assertIsUnmodifiable()
SELF
contains(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map contains the given entries, in any order.SELF
containsAllEntriesOf(java.util.Map<? extends K,? extends V> other)
Verifies that the actual map contains all entries of the given map, in any order.SELF
containsAnyOf(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map contains at least one of the given entries.protected SELF
containsAnyOfForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
SELF
containsEntry(K key, V value)
Verifies that the actual map contains the given entry.SELF
containsExactly(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map contains only the given entries and nothing else, in order.
This assertion should only be used with maps that have a consistent iteration order (i.e.SELF
containsExactlyEntriesOf(java.util.Map<? extends K,? extends V> map)
Same ascontainsExactly(Map.Entry[])
but handles the conversion ofMap.entrySet()
to array.protected SELF
containsExactlyForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
SELF
containsExactlyInAnyOrderEntriesOf(java.util.Map<? extends K,? extends V> map)
Same ascontainsOnly(Map.Entry[])
but handles the conversion ofMap.entrySet()
to array.protected SELF
containsForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
SELF
containsKey(K key)
Verifies that the actual map contains the given key.SELF
containsKeys(K... keys)
Verifies that the actual map contains the given keys.protected SELF
containsKeysForProxy(K[] keys)
SELF
containsOnly(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map contains only the given entries and nothing else, in any order.protected SELF
containsOnlyForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
SELF
containsOnlyKeys(java.lang.Iterable<? extends K> keys)
Verifies that the actual map contains only the given keys and nothing else, in any order.SELF
containsOnlyKeys(K... keys)
Verifies that the actual map contains only the given keys and nothing else, in any order.protected SELF
containsOnlyKeysForProxy(K[] keys)
SELF
containsValue(V value)
Verifies that the actual map contains the given value.SELF
containsValues(V... values)
Verifies that the actual map contains the given values.protected SELF
containsValuesForProxy(V[] values)
SELF
describedAs(java.lang.String description, java.lang.Object... args)
Sets the description of the assertion that is going to be called after.SELF
describedAs(Description description)
Sets the description of the assertion that is going to be called after.SELF
doesNotContain(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map does not contain the given entries.SELF
doesNotContainEntry(K key, V value)
Verifies that the actual map does not contain the given entry.protected SELF
doesNotContainForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
SELF
doesNotContainKey(K key)
Verifies that the actual map does not contain the given key.SELF
doesNotContainKeys(K... keys)
Verifies that the actual map does not contain any of the given keys.protected SELF
doesNotContainKeysForProxy(K[] keys)
SELF
doesNotContainValue(V value)
Verifies that the actual map does not contain the given value.SELF
doesNotHave(Condition<? super ACTUAL> condition)
Verifies that the actual value does not satisfy the given condition.SELF
doesNotHaveSameClassAs(java.lang.Object other)
Verifies that the actual value does not have the same class as the given object.private void
expectUnsupportedOperationException(java.lang.Runnable runnable, java.lang.String method)
AbstractObjectAssert<?,?>
extracting(java.lang.Object key)
Deprecated.useextractingByKey(Object)
insteadAbstractListAssert<?,java.util.List<?>,java.lang.Object,ObjectAssert<java.lang.Object>>
extracting(java.lang.Object... keys)
Deprecated.useextractingByKeys(Object[])
insteadAbstractObjectAssert<?,V>
extractingByKey(K key)
Extract the value of given key from the map under test, the extracted value becoming the new object under test.<ASSERT extends AbstractAssert<?,?>>
ASSERTextractingByKey(K key, InstanceOfAssertFactory<?,ASSERT> assertFactory)
Extract the value of given key from the map under test, the extracted value becoming the new object under test.AbstractListAssert<?,java.util.List<? extends V>,V,ObjectAssert<V>>
extractingByKeys(K... keys)
Extract the values of given keys from the map under test into an array, this new array becoming the object under test.protected AbstractListAssert<?,java.util.List<? extends V>,V,ObjectAssert<V>>
extractingByKeysForProxy(K[] keys)
AbstractListAssert<?,java.util.List<?>,java.lang.Object,ObjectAssert<java.lang.Object>>
extractingFromEntries(java.util.function.Function<? super java.util.Map.Entry<K,V>,java.lang.Object> extractor)
Use the givenFunction
to extract a value from theMap
's entries.AbstractListAssert<?,java.util.List<? extends Tuple>,Tuple,ObjectAssert<Tuple>>
extractingFromEntries(java.util.function.Function<? super java.util.Map.Entry<K,V>,java.lang.Object>... extractors)
Use the givenFunction
s to extract values from theMap
's entries.protected AbstractListAssert<?,java.util.List<? extends Tuple>,Tuple,ObjectAssert<Tuple>>
extractingFromEntriesForProxy(java.util.function.Function<? super java.util.Map.Entry<K,V>,java.lang.Object>[] extractors)
AbstractListAssert<?,java.util.List<?>,java.lang.Object,ObjectAssert<java.lang.Object>>
flatExtracting(java.lang.String... keys)
Flatten the values of the given keys from the actual map under test into a new array, this new array becoming the object under test.private static java.util.List<java.lang.Object>
flatten(java.lang.Iterable<java.lang.Object> collectionToFlatten)
SELF
has(Condition<? super ACTUAL> condition)
Verifies that the actual value satisfies the given condition.SELF
hasEntrySatisfying(K key, java.util.function.Consumer<? super V> valueRequirements)
Verifies that the actual map contains the value for givenkey
that satisfy givenvalueRequirements
.SELF
hasEntrySatisfying(K key, Condition<? super V> valueCondition)
Verifies that the actual map contains a value for the givenkey
that satisfies the givenvalueCondition
.SELF
hasEntrySatisfying(Condition<? super java.util.Map.Entry<K,V>> entryCondition)
Verifies that the actual map contains an entry satisfying the givenentryCondition
.SELF
hasEntrySatisfying(Condition<? super K> keyCondition, Condition<? super V> valueCondition)
Verifies that the actual map contains an entry with a key satisfying the givenkeyCondition
and a value satisfying the givenvalueCondition
.SELF
hasKeySatisfying(Condition<? super K> keyCondition)
Verifies that the actual map contains an entry with a key satisfying the givenkeyCondition
.SELF
hasSameClassAs(java.lang.Object other)
Verifies that the actual value has the same class as the given object.SELF
hasSameSizeAs(java.lang.Iterable<?> other)
Verifies that the actual map has the same size as the givenIterable
.SELF
hasSameSizeAs(java.lang.Object other)
Verifies that the actual map has the same size as the given array.SELF
hasSameSizeAs(java.util.Map<?,?> other)
Verifies that the actual map has the same size as the givenMap
.SELF
hasSize(int expected)
Verifies that the number of values in theMap
is equal to the given one.SELF
hasSizeBetween(int lowerBoundary, int higherBoundary)
Verifies that the number of values in theMap
is between the given boundaries (inclusive).SELF
hasSizeGreaterThan(int boundary)
Verifies that the number of values in theMap
is greater than the boundary.SELF
hasSizeGreaterThanOrEqualTo(int boundary)
Verifies that the number of values in theMap
is greater than or equal to the boundary.SELF
hasSizeLessThan(int boundary)
Verifies that the number of values in theMap
is less than the boundary.SELF
hasSizeLessThanOrEqualTo(int boundary)
Verifies that the number of values in theMap
is less than or equal to the boundary.SELF
hasToString(java.lang.String expectedToString)
Verifies that actualactual.toString()
is equal to the givenString
.SELF
hasValueSatisfying(Condition<? super V> valueCondition)
Verifies that the actual map contains an entry with a value satisfying the givenvalueCondition
.private AbstractObjectAssert<?,V>
internalExtractingByKey(K key)
SELF
is(Condition<? super ACTUAL> condition)
Verifies that the actual value satisfies the given condition.void
isEmpty()
Verifies that theMap
is empty.SELF
isEqualTo(java.lang.Object expected)
Verifies that the actual value is equal to the given one.SELF
isExactlyInstanceOf(java.lang.Class<?> type)
Verifies that the actual value is exactly an instance of the given type.SELF
isIn(java.lang.Iterable<?> values)
Verifies that the actual value is present in the given iterable.SELF
isIn(java.lang.Object... values)
Verifies that the actual value is present in the given array of values.SELF
isInstanceOf(java.lang.Class<?> type)
Verifies that the actual value is an instance of the given type.SELF
isInstanceOfAny(java.lang.Class<?>... types)
Verifies that the actual value is an instance of any of the given types.SELF
isNot(Condition<? super ACTUAL> condition)
Verifies that the actual value does not satisfy the given condition.SELF
isNotEmpty()
Verifies that theMap
is not empty.SELF
isNotEqualTo(java.lang.Object other)
Verifies that the actual value is not equal to the given one.SELF
isNotExactlyInstanceOf(java.lang.Class<?> type)
Verifies that the actual value is not exactly an instance of given type.SELF
isNotIn(java.lang.Iterable<?> values)
Verifies that the actual value is not present in the given iterable.SELF
isNotIn(java.lang.Object... values)
Verifies that the actual value is not present in the given array of values.SELF
isNotInstanceOf(java.lang.Class<?> type)
Verifies that the actual value is not an instance of the given type.SELF
isNotInstanceOfAny(java.lang.Class<?>... types)
Verifies that the actual value is not an instance of any of the given types.SELF
isNotNull()
Verifies that the actual value is notnull
.SELF
isNotOfAnyClassIn(java.lang.Class<?>... types)
Verifies that the actual value type is not in given types.SELF
isNotSameAs(java.lang.Object other)
Verifies that the actual value is not the same as the given one, ie using == comparison.void
isNullOrEmpty()
Verifies that theMap
isnull
or empty.SELF
isOfAnyClassIn(java.lang.Class<?>... types)
Verifies that the actual value type is in given types.SELF
isSameAs(java.lang.Object expected)
Verifies that the actual value is the same as the given one, ie using == comparison.SELF
isUnmodifiable()
Verifies that the actual map is unmodifiable, i.e.SELF
noneSatisfy(java.util.function.BiConsumer<? super K,? super V> entryRequirements)
Verifies that no map entry satisfies the givenentryRequirements
.SELF
overridingErrorMessage(java.lang.String newErrorMessage, java.lang.Object... args)
Overrides AssertJ default error message by the given one.AbstractMapSizeAssert<SELF,ACTUAL,K,V>
size()
Returns anAssert
object that allows performing assertions on the size of theMap
under test.private java.util.Map.Entry<? extends K,? extends V>[]
toEntries(java.util.Map<? extends K,? extends V> map)
SELF
usingComparator(java.util.Comparator<? super ACTUAL> customComparator)
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.SELF
usingComparator(java.util.Comparator<? super ACTUAL> customComparator, java.lang.String customComparatorDescription)
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.SELF
usingDefaultComparator()
Revert to standard comparison for the incoming assertion checks.SELF
usingDefaultElementComparator()
Deprecated.Custom element Comparator is not supported for MapEntry comparison.SELF
usingElementComparator(java.util.Comparator<? super java.util.Map.Entry<? extends K,? extends V>> customComparator)
Deprecated.Custom element Comparator is not supported for MapEntry comparison.RecursiveAssertionAssert
usingRecursiveAssertion()
Asserts that the given predicate is met for all fields of the object under test recursively (but not the object itself).RecursiveAssertionAssert
usingRecursiveAssertion(RecursiveAssertionConfiguration recursiveAssertionConfiguration)
The same asusingRecursiveAssertion()
, but this method allows the developer to pass in an explicit recursion configuration.RecursiveComparisonAssert<?>
usingRecursiveComparison()
Enable using a recursive field by field comparison strategy when calling the chainedRecursiveComparisonAssert
,RecursiveComparisonAssert<?>
usingRecursiveComparison(RecursiveComparisonConfiguration recursiveComparisonConfiguration)
Same asusingRecursiveComparison()
but allows to specify your ownRecursiveComparisonConfiguration
.SELF
withFailMessage(java.lang.String newErrorMessage, java.lang.Object... args)
Alternative method forAbstractAssert.overridingErrorMessage(java.lang.String, java.lang.Object...)
SELF
withThreadDumpOnError()
In case of an assertion error, a thread dump will be printed toSystem.err
.-
Methods inherited from class org.assertj.core.api.AbstractObjectAssert
doesNotReturn, extracting, extracting, extracting, extracting, extracting, extracting, extractingForProxy, getComparatorsByType, hasAllNullFieldsOrProperties, hasAllNullFieldsOrPropertiesExcept, hasFieldOrProperty, hasFieldOrPropertyWithValue, hasNoNullFieldsOrProperties, hasNoNullFieldsOrPropertiesExcept, hasOnlyFields, isEqualToComparingFieldByField, isEqualToComparingFieldByFieldRecursively, isEqualToComparingOnlyGivenFields, isEqualToIgnoringGivenFields, isEqualToIgnoringNullFields, newObjectAssert, returns, usingComparatorForFields, usingComparatorForType, withAssertionState, withComparatorByPropertyOrField, withTypeComparator
-
Methods inherited from class org.assertj.core.api.AbstractAssert
areEqual, asInstanceOf, asList, assertionError, asString, descriptionText, doesNotHaveSameHashCodeAs, doesNotHaveToString, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, hashCode, hasSameHashCodeAs, inBinary, inHexadecimal, isElementOfCustomAssert, isInstanceOfSatisfying, isNull, matches, matches, newListAssertInstance, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, withFailMessage, withRepresentation
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.assertj.core.api.Descriptable
as
-
-
-
-
Field Detail
-
maps
Maps maps
-
-
Constructor Detail
-
AbstractMapAssert
protected AbstractMapAssert(ACTUAL actual, java.lang.Class<?> selfType)
-
-
Method Detail
-
allSatisfy
public SELF allSatisfy(java.util.function.BiConsumer<? super K,? super V> entryRequirements)
Verifies that all the actual map entries satisfy the givenentryRequirements
.Example:
Map<TolkienCharacter, Ring> elvesRingBearers = new HashMap<>(); elvesRingBearers.put(galadriel, nenya); elvesRingBearers.put(gandalf, narya); elvesRingBearers.put(elrond, vilya); // this assertion succeeds: assertThat(elvesRingBearers).allSatisfy((character, ring) -> { assertThat(character.getRace()).isIn(ELF, MAIA); assertThat(ring).isIn(nenya, narya, vilya); }); // this assertion fails as Gandalf is a maia and not an elf: assertThat(elvesRingBearers).allSatisfy((character, ring) -> { assertThat(character.getRace()).isEqualTo(ELF); assertThat(ring).isIn(nenya, narya, vilya); });
If the actual map is empty, this assertion succeeds as there is nothing to check.
- Parameters:
entryRequirements
- the given requirements that each entry must satisfy.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given entryRequirementsBiConsumer
isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if one or more entries don't satisfy the given requirements.- Since:
- 3.9.0
-
anySatisfy
public SELF anySatisfy(java.util.function.BiConsumer<? super K,? super V> entryRequirements)
Verifies that at least one map entry satisfies the givenentryRequirements
.Example:
Map<TolkienCharacter, Ring> elvesRingBearers = new HashMap<>(); elvesRingBearers.put(galadriel, nenya); elvesRingBearers.put(gandalf, narya); elvesRingBearers.put(elrond, vilya); // this assertion succeeds as gandalf is a maia wearing narya: assertThat(elvesRingBearers).anySatisfy((character, ring) -> { assertThat(character.getRace()).isEqualTo(MAIA); assertThat(ring).isEqualTo(narya); }); // this assertion fails, gandalf is a maia but he does not wear the One Ring: assertThat(elvesRingBearers).anySatisfy((character, ring) -> { assertThat(character.getRace()).isIn(MAIA, HOBBIT); assertThat(ring).isEqualTo(oneRing); });
- Parameters:
entryRequirements
- the given requirements that at least one entry must satisfy.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given entryRequirementsBiConsumer
isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if no entries satisfy the given requirements.- Since:
- 3.12.0
-
noneSatisfy
public SELF noneSatisfy(java.util.function.BiConsumer<? super K,? super V> entryRequirements)
Verifies that no map entry satisfies the givenentryRequirements
.Example:
Map<TolkienCharacter, Ring> elvesRingBearers = new HashMap<>(); elvesRingBearers.put(galadriel, nenya); elvesRingBearers.put(gandalf, narya); elvesRingBearers.put(elrond, vilya); // this assertion succeeds: assertThat(elvesRingBearers).noneSatisfy((character, ring) -> { assertThat(character.getRace()).isIn(HOBBIT, DWARF);M assertThat(ring).isIn(nenya, narya, vilya); }); // this assertion fails as Gandalf is a maia. assertThat(elvesRingBearers).noneSatisfy((character, ring) -> { assertThat(character.getRace()).isEqualTo(MAIA); assertThat(ring).isIn(nenya, narya, vilya); });
If the actual map is empty, this assertion succeeds as there is nothing to check.
- Parameters:
entryRequirements
- the given requirements that each entry must not satisfy.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given entryRequirementsBiConsumer
isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if one or more entries satisfies the given requirements.- Since:
- 3.12.0
-
isNullOrEmpty
public void isNullOrEmpty()
Verifies that theMap
isnull
or empty.Example:
// assertions will pass Map<Integer, String> map = null; assertThat(map).isNullOrEmpty(); assertThat(new HashMap()).isNullOrEmpty(); // assertion will fail Map<String, String> keyToValue = new HashMap(); keyToValue.put("key", "value"); assertThat(keyToValue).isNullOrEmpty()
- Specified by:
isNullOrEmpty
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Throws:
java.lang.AssertionError
- if theMap
is notnull
or not empty.
-
isEmpty
public void isEmpty()
Verifies that theMap
is empty.Example:
// assertion will pass assertThat(new HashMap()).isEmpty(); // assertion will fail Map<String, String> map = new HashMap(); map.put("key", "value"); assertThat(map).isEmpty();
-
isNotEmpty
public SELF isNotEmpty()
Verifies that theMap
is not empty.Example:
Map<String, String> map = new HashMap(); map.put("key", "value"); // assertion will pass assertThat(map).isNotEmpty(); // assertion will fail assertThat(new HashMap()).isNotEmpty();
- Specified by:
isNotEmpty
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if theMap
is empty.
-
hasSize
public SELF hasSize(int expected)
Verifies that the number of values in theMap
is equal to the given one.Example:
Map<String, String> map = new HashMap(); map.put("key", "value"); // assertion will pass assertThat(map).hasSize(1); // assertions will fail assertThat(map).hasSize(0); assertThat(map).hasSize(2);
- Specified by:
hasSize
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
expected
- the expected number of values in theMap
.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the number of values of theMap
is not equal to the given one.
-
hasSizeGreaterThan
public SELF hasSizeGreaterThan(int boundary)
Verifies that the number of values in theMap
is greater than the boundary.Example:
Map<String, String> map = new HashMap(); map.put("key", "value"); map.put("key2", "value2"); // assertion will pass assertThat(map).hasSizeGreaterThan(1); // assertions will fail assertThat(map).hasSizeGreaterThan(3);
- Specified by:
hasSizeGreaterThan
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
boundary
- the given value to compare the size ofactual
to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the number of values of theMap
is not greater than the boundary.- Since:
- 3.12.0
-
hasSizeGreaterThanOrEqualTo
public SELF hasSizeGreaterThanOrEqualTo(int boundary)
Verifies that the number of values in theMap
is greater than or equal to the boundary.Example:
Map<String, String> map = new HashMap(); map.put("key", "value"); map.put("key2", "value2"); // assertions will pass assertThat(map).hasSizeGreaterThanOrEqualTo(1) .hasSizeGreaterThanOrEqualTo(2); // assertions will fail assertThat(map).hasSizeGreaterThanOrEqualTo(3); assertThat(map).hasSizeGreaterThanOrEqualTo(5);
- Specified by:
hasSizeGreaterThanOrEqualTo
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
boundary
- the given value to compare the size ofactual
to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the number of values of theMap
is not greater than or equal to the boundary.- Since:
- 3.12.0
-
hasSizeLessThan
public SELF hasSizeLessThan(int boundary)
Verifies that the number of values in theMap
is less than the boundary.Example:
Map<String, String> map = new HashMap(); map.put("key", "value"); map.put("key2", "value2"); // assertion will pass assertThat(map).hasSizeLessThan(3); // assertions will fail assertThat(map).hasSizeLessThan(1); assertThat(map).hasSizeLessThan(2);
- Specified by:
hasSizeLessThan
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
boundary
- the given value to compare the size ofactual
to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the number of values of theMap
is not less than the boundary.- Since:
- 3.12.0
-
hasSizeLessThanOrEqualTo
public SELF hasSizeLessThanOrEqualTo(int boundary)
Verifies that the number of values in theMap
is less than or equal to the boundary.Example:
Map<String, String> map = new HashMap(); map.put("key", "value"); map.put("key2", "value2"); // assertions will pass assertThat(map).hasSizeLessThanOrEqualTo(2) .hasSizeLessThanOrEqualTo(3); // assertions will fail assertThat(map).hasSizeLessThanOrEqualTo(0); assertThat(map).hasSizeLessThanOrEqualTo(1);
- Specified by:
hasSizeLessThanOrEqualTo
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
boundary
- the given value to compare the size ofactual
to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the number of values of theMap
is not less than the boundary.- Since:
- 3.12.0
-
hasSizeBetween
public SELF hasSizeBetween(int lowerBoundary, int higherBoundary)
Verifies that the number of values in theMap
is between the given boundaries (inclusive).Example:
Map<String, String> map = new HashMap(); map.put("key", "value"); map.put("key2", "value2"); // assertions will pass assertThat(map).hasSizeBetween(1, 3) .hasSizeBetween(2, 2); // assertions will fail assertThat(map).hasSizeBetween(3, 4);
- Specified by:
hasSizeBetween
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
lowerBoundary
- the lower boundary compared to which actual size should be greater than or equal to.higherBoundary
- the higher boundary compared to which actual size should be less than or equal to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the number of values of theMap
is not between the boundaries.- Since:
- 3.12.0
-
hasSameSizeAs
public SELF hasSameSizeAs(java.lang.Object other)
Verifies that the actual map has the same size as the given array.Parameter is declared as Object to accept both Object[] and primitive arrays (e.g. int[]).
Example:
int[] oneTwoThree = {1, 2, 3}; Map<Ring, TolkienCharacter> elvesRingBearers = new HashMap<>(); elvesRingBearers.put(nenya, galadriel); elvesRingBearers.put(narya, gandalf); elvesRingBearers.put(vilya, elrond); // assertion will pass assertThat(elvesRingBearers).hasSameSizeAs(oneTwoThree); // assertions will fail assertThat(elvesRingBearers).hasSameSizeAs(new int[] {1}); assertThat(keyToValue).hasSameSizeAs(new char[] {'a', 'b', 'c', 'd'});
- Specified by:
hasSameSizeAs
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
other
- the array to compare size with actual group.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actual group isnull
.java.lang.AssertionError
- if the array parameter isnull
or is not a true array.java.lang.AssertionError
- if actual group and given array don't have the same size.
-
hasSameSizeAs
public SELF hasSameSizeAs(java.lang.Iterable<?> other)
Verifies that the actual map has the same size as the givenIterable
.Examples:
Map<Ring, TolkienCharacter> elvesRingBearers = new HashMap<>(); elvesRingBearers.put(nenya, galadriel); elvesRingBearers.put(narya, gandalf); elvesRingBearers.put(vilya, elrond); // assertion will pass assertThat(elvesRingBearers).hasSameSizeAs(Array.asList(vilya, nenya, narya)); // assertions will fail assertThat(elvesRingBearers).hasSameSizeAs(Array.asList(1)); assertThat(keyToValue).hasSameSizeAs(Array.asList('a', 'b', 'c', 'd'));
- Specified by:
hasSameSizeAs
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
other
- theIterable
to compare size with actual group.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the otherIterable
isnull
.java.lang.AssertionError
- if the actual map and the givenIterable
don't have the same size
-
hasSameSizeAs
public SELF hasSameSizeAs(java.util.Map<?,?> other)
Verifies that the actual map has the same size as the givenMap
.Examples:
import static com.google.common.collect.ImmutableMap.of; Map<Ring, TolkienCharacter> ringBearers = ImmutableMap.of(nenya, galadriel, narya, gandalf, vilya, elrond, oneRing, frodo); // assertion succeeds: assertThat(ringBearers).hasSameSizeAs(ImmutableMap.of(oneRing, frodo, narya, gandalf, nenya, galadriel, vilya, elrond)); // assertions fails: assertThat(ringBearers).hasSameSizeAs(Collections.emptyMap()); assertThat(ringBearers).hasSameSizeAs(ImmutableMap.of(nenya, galadriel, narya, gandalf, vilya, elrond));
- Parameters:
other
- theMap
to compare size with actual map- Returns:
this
assertion object- Throws:
java.lang.NullPointerException
- if the otherMap
isnull
java.lang.AssertionError
- if the actual map isnull
java.lang.AssertionError
- if the actual map and the givenMap
don't have the same size
-
contains
@SafeVarargs public final SELF contains(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map contains the given entries, in any order.This assertion succeeds if both actual map and given entries are empty.
Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertions will pass assertThat(ringBearers).contains(entry(oneRing, frodo), entry(nenya, galadriel)); assertThat(emptyMap).contains(); // assertions will fail assertThat(ringBearers).contains(entry(oneRing, sauron)); assertThat(ringBearers).contains(entry(oneRing, sauron), entry(oneRing, aragorn)); assertThat(ringBearers).contains(entry(narya, gandalf), entry(oneRing, sauron));
- Parameters:
entries
- the given entries.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given argument isnull
.java.lang.NullPointerException
- if any of the entries in the given array isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map does not contain the given entries.
-
containsForProxy
protected SELF containsForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
-
containsAnyOf
@SafeVarargs public final SELF containsAnyOf(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map contains at least one of the given entries.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertions will pass assertThat(ringBearers).containsAnyOf(entry(oneRing, frodo), entry(oneRing, sauron)); assertThat(emptyMap).containsAnyOf(); // assertion will fail assertThat(ringBearers).containsAnyOf(entry(oneRing, gandalf), entry(oneRing, aragorn));
- Parameters:
entries
- the given entries.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given argument isnull
.java.lang.IllegalArgumentException
- if the given argument is an empty array.java.lang.NullPointerException
- if any of the entries in the given array isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map does not contain any of the given entries.- Since:
- 3.6.0
-
containsAnyOfForProxy
protected SELF containsAnyOfForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
-
containsAllEntriesOf
public SELF containsAllEntriesOf(java.util.Map<? extends K,? extends V> other)
Verifies that the actual map contains all entries of the given map, in any order.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); Map<Ring, TolkienCharacter> elvesRingBearers = new HashMap<>(); elvesRingBearers.put(nenya, galadriel); elvesRingBearers.put(narya, gandalf); elvesRingBearers.put(vilya, elrond); // assertions succeed assertThat(ringBearers).containsAllEntriesOf(elvesRingBearers); assertThat(ringBearers).containsAllEntriesOf(emptyMap); // assertion fails assertThat(elvesRingBearers).containsAllEntriesOf(ringBearers);
- Parameters:
other
- the map with the given entries.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given argument isnull
.java.lang.NullPointerException
- if any of the entries in the given map isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map does not contain the given entries.
-
containsExactlyEntriesOf
public SELF containsExactlyEntriesOf(java.util.Map<? extends K,? extends V> map)
Same ascontainsExactly(Map.Entry[])
but handles the conversion ofMap.entrySet()
to array.Verifies that the actual map contains only the entries of the given map and nothing else, in order.
This assertion should only be used with maps that have a consistent iteration order (i.e. don't use it withHashMap
, prefercontainsExactlyInAnyOrderEntriesOf(java.util.Map)
in that case).Examples:
// newLinkedHashMap builds a Map with iteration order corresponding to the insertion order Map<Ring, TolkienCharacter> ringBearers = newLinkedHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf)); // assertion will pass assertThat(ringBearers).containsExactlyEntriesOf(newLinkedHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf))); // assertion will fail as actual and expected order differ assertThat(ringBearers).containsExactlyEntriesOf(newLinkedHashMap(entry(nenya, galadriel), entry(narya, gandalf), entry(oneRing, frodo))); // assertion will fail as actual and expected have different sizes assertThat(ringBearers).containsExactlyEntriesOf(newLinkedHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf), entry(narya, gandalf)));
- Parameters:
map
- the givenMap
with the expected entries to be found in actual.- Returns:
this
assertions object- Throws:
java.lang.NullPointerException
- if the given map isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.IllegalArgumentException
- if the given map is empty.java.lang.AssertionError
- if the actual map does not contain the entries of the given map with same order, i.e the actual map contains some or none of the entries of the given map, or the actual map contains more entries than the entries of the given map or entries are the same but the order is not.- Since:
- 3.12.0
-
containsExactlyInAnyOrderEntriesOf
public SELF containsExactlyInAnyOrderEntriesOf(java.util.Map<? extends K,? extends V> map)
Same ascontainsOnly(Map.Entry[])
but handles the conversion ofMap.entrySet()
to array.Verifies that the actual map contains only the given entries and nothing else, in any order.
Examples:
// newLinkedHashMap builds a Map with iteration order corresponding to the insertion order Map<Ring, TolkienCharacter> ringBearers = newLinkedHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf)); // assertion will pass assertThat(ringBearers).containsExactlyInAnyOrderEntriesOf(newLinkedHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf))); // assertion will pass although actual and expected order differ assertThat(ringBearers).containsExactlyInAnyOrderEntriesOf(newLinkedHashMap(entry(nenya, galadriel), entry(narya, gandalf), entry(oneRing, frodo))); // assertion will fail as actual does not contain all expected entries assertThat(ringBearers).containsExactlyInAnyOrderEntriesOf(newLinkedHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(vilya, elrond))); // assertion will fail as actual and expected have different sizes assertThat(ringBearers).containsExactlyInAnyOrderEntriesOf(newLinkedHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf), entry(narya, gandalf)));
- Parameters:
map
- the givenMap
with the expected entries to be found in actual.- Returns:
this
assertions object- Throws:
java.lang.NullPointerException
- if the given map isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.IllegalArgumentException
- if the given map is empty.java.lang.AssertionError
- if the actual map does not contain the entries of the given map, i.e the actual map contains some or none of the entries of the given map, or the actual map contains more entries than the entries of the given map.- Since:
- 3.13.0
-
toEntries
private java.util.Map.Entry<? extends K,? extends V>[] toEntries(java.util.Map<? extends K,? extends V> map)
-
containsEntry
public SELF containsEntry(K key, V value)
Verifies that the actual map contains the given entry.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertions will pass assertThat(ringBearers).containsEntry(oneRing, frodo).containsEntry(nenya, galadriel); // assertion will fail assertThat(ringBearers).containsEntry(oneRing, sauron);
- Parameters:
key
- the given key to check.value
- the given value to check.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given argument isnull
.java.lang.IllegalArgumentException
- if the given argument is an empty array.java.lang.NullPointerException
- if any of the entries in the given array isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map does not contain the given entries.
-
hasEntrySatisfying
public SELF hasEntrySatisfying(K key, Condition<? super V> valueCondition)
Verifies that the actual map contains a value for the givenkey
that satisfies the givenvalueCondition
.Example:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); Condition<TolkienCharacter> elfBearer = new Condition<>("an elf bearer") { public boolean matches(TolkienCharacter character) { return character.getRace() == ELF; } }; // this assertion will pass assertThat(ringBearers).hasEntrySatisfying(nenya, elfBearer); // this assertion will fail assertThat(ringBearers).hasEntrySatisfying(oneRing, elfBearer);
- Parameters:
key
- he given key to check.valueCondition
- the given condition for check value.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given values isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map not contains the givenkey
.java.lang.AssertionError
- if the actual map contains the given key, but value not match the givenvalueCondition
.- Since:
- 2.6.0 / 3.6.0
-
hasEntrySatisfying
public SELF hasEntrySatisfying(K key, java.util.function.Consumer<? super V> valueRequirements)
Verifies that the actual map contains the value for givenkey
that satisfy givenvalueRequirements
.Example:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // this assertion will pass assertThat(ringBearers).hasEntrySatisfying(nenya, character -> { assertThat(character.getName()).contains("driel"); assertThat(character.getRace()).isEqualTo(ELF); }); // this assertion will fail assertThat(ringBearers).hasEntrySatisfying(oneRing, character -> { assertThat(character.getRace()).isEqualTo(ELF); });
- Parameters:
key
- he given key to check.valueRequirements
- the given requirements for check value.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given values isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map not contains the givenkey
.java.lang.AssertionError
- if the actual map contains the given key, but value not pass the givenvalueRequirements
.- Since:
- 3.6.0
-
hasEntrySatisfying
public SELF hasEntrySatisfying(Condition<? super java.util.Map.Entry<K,V>> entryCondition)
Verifies that the actual map contains an entry satisfying the givenentryCondition
.Example:
Map<TolkienCharacter, Ring> ringBearers = new HashMap<>(); ringBearers.put(galadriel, nenya); ringBearers.put(gandalf, narya); ringBearers.put(elrond, vilya); ringBearers.put(frodo, oneRing); Condition<Map.Entry<TolkienCharacter, Ring>> oneRingManBearer = new Condition<Map.Entry<TolkienCharacter, Ring>>("One ring man bearer") { public boolean matches(Map.Entry<TolkienCharacter, Ring> entry) { return entry.getKey().getRace() == MAN && entry.getValue() == oneRing; } }; // assertion will fail assertThat(ringBearers).hasEntrySatisfying(oneRingManBearer); ringBearers.put(isildur, oneRing); // now assertion will pass assertThat(ringBearers).hasEntrySatisfying(oneRingManBearer);
- Parameters:
entryCondition
- the condition for searching entry.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given condition isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if there is no entry matching givenentryCondition
.- Since:
- 2.7.0 / 3.7.0
-
hasEntrySatisfying
public SELF hasEntrySatisfying(Condition<? super K> keyCondition, Condition<? super V> valueCondition)
Verifies that the actual map contains an entry with a key satisfying the givenkeyCondition
and a value satisfying the givenvalueCondition
.Example:
Map<TolkienCharacter, Ring> ringBearers = new HashMap<>(); ringBearers.put(galadriel, nenya); ringBearers.put(gandalf, narya); ringBearers.put(elrond, vilya); ringBearers.put(frodo, oneRing); Condition<TolkienCharacter> isMan = new Condition<TolkienCharacter>("is man") { public boolean matches(TolkienCharacter tolkienCharacter) { return tolkienCharacter.getRace() == MAN; } }; Condition<Ring> oneRingBearer = new Condition<Ring>("One ring bearer") { public boolean matches(Ring ring) { return ring == oneRing; } }; // assertion will fail assertThat(ringBearers).hasEntrySatisfying(isMan, oneRingBearer); ringBearers.put(isildur, oneRing); // now assertion will pass assertThat(ringBearers).hasEntrySatisfying(isMan, oneRingBearer);
- Parameters:
keyCondition
- the condition to be matched by the entry's key.valueCondition
- the condition to be matched by the entry's value.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if any of the given conditions isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if there is no entry with a key matchingkeyCondition
and a value matchingvalueCondition
.- Since:
- 2.7.0 / 3.7.0
-
hasKeySatisfying
public SELF hasKeySatisfying(Condition<? super K> keyCondition)
Verifies that the actual map contains an entry with a key satisfying the givenkeyCondition
.Example:
Map<TolkienCharacter, Ring> ringBearers = new HashMap<>(); ringBearers.put(galadriel, nenya); ringBearers.put(gandalf, narya); ringBearers.put(elrond, vilya); ringBearers.put(frodo, oneRing); Condition<TolkienCharacter> isElf = new Condition<TolkienCharacter>("is elf") { public boolean matches(TolkienCharacter tolkienCharacter) { return tolkienCharacter.getRace() == ELF; } }; Condition<TolkienCharacter> isOrc = new Condition<TolkienCharacter>("is orc") { public boolean matches(TolkienCharacter tolkienCharacter) { return tolkienCharacter.getRace() == ORC; } }; // assertion will pass assertThat(ringBearers).hasKeySatisfying(isElf); // assertion will fail assertThat(ringBearers).hasKeySatisfying(isOrc);
- Parameters:
keyCondition
- the condition to be matched by the entry's key.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given condition isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if there is no key matching the givenkeyCondition
.- Since:
- 2.7.0 / 3.7.0
-
hasValueSatisfying
public SELF hasValueSatisfying(Condition<? super V> valueCondition)
Verifies that the actual map contains an entry with a value satisfying the givenvalueCondition
.Example:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); Condition<TolkienCharacter> isElf = new Condition<TolkienCharacter>("is elf") { public boolean matches(TolkienCharacter tolkienCharacter) { return tolkienCharacter.getRace() == ELF; } }; Condition<TolkienCharacter> isOrc = new Condition<TolkienCharacter>("is orc") { public boolean matches(TolkienCharacter tolkienCharacter) { return tolkienCharacter.getRace() == ORC; } }; // assertion will pass assertThat(ringBearers).hasValueSatisfying(isElf); // assertion will fail assertThat(ringBearers).hasValueSatisfying(isOrc);
- Parameters:
valueCondition
- the condition to be matched by the entry's value.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given condition isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if there is no value matching the givenvalueCondition
.- Since:
- 2.7.0 / 3.7.0
-
doesNotContain
@SafeVarargs public final SELF doesNotContain(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map does not contain the given entries.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertion will pass assertThat(ringBearers).doesNotContain(entry(oneRing, aragorn), entry(oneRing, sauron)); // assertions will fail assertThat(ringBearers).doesNotContain(entry(oneRing, frodo)); assertThat(ringBearers).doesNotContain(entry(oneRing, frodo), entry(oneRing, aragorn));
- Parameters:
entries
- the given entries.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given argument isnull
.java.lang.IllegalArgumentException
- if the given argument is an empty array.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map contains any of the given entries.
-
doesNotContainForProxy
protected SELF doesNotContainForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
-
doesNotContainEntry
public SELF doesNotContainEntry(K key, V value)
Verifies that the actual map does not contain the given entry.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertion will pass assertThat(ringBearers).doesNotContainEntry(oneRing, aragorn); // assertion will fail assertThat(ringBearers).doesNotContain(oneRing, frodo);
- Parameters:
key
- key of the entry.value
- value of the entry.- Returns:
this
assertion object.- Throws:
java.lang.NullPointerException
- if the given argument isnull
.java.lang.IllegalArgumentException
- if the given argument is an empty array.java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map contains any of the given entries.
-
containsKey
public SELF containsKey(K key)
Verifies that the actual map contains the given key.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); // assertion will pass assertThat(ringBearers).containsKey(vilya); // assertion will fail assertThat(ringBearers).containsKey(oneRing);
- Parameters:
key
- the given key- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map does not contain the given key.
-
containsKeys
@SafeVarargs public final SELF containsKeys(K... keys)
Verifies that the actual map contains the given keys.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(oneRing, frodo); // assertions will pass assertThat(ringBearers).containsKeys(nenya, oneRing); // assertions will fail assertThat(ringBearers).containsKeys(vilya); assertThat(ringBearers).containsKeys(vilya, oneRing);
- Parameters:
keys
- the given keys- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map does not contain the given key.java.lang.IllegalArgumentException
- if the given argument is an empty array.
-
doesNotContainKey
public SELF doesNotContainKey(K key)
Verifies that the actual map does not contain the given key.Examples:
Map<Ring, TolkienCharacter> elvesRingBearers = new HashMap<>(); elvesRingBearers.put(nenya, galadriel); elvesRingBearers.put(narya, gandalf); elvesRingBearers.put(vilya, elrond); // assertion will pass assertThat(elvesRingBearers).doesNotContainKey(oneRing); // assertion will fail assertThat(elvesRingBearers).doesNotContainKey(vilya);
- Parameters:
key
- the given key- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map contains the given key.
-
doesNotContainKeys
@SafeVarargs public final SELF doesNotContainKeys(K... keys)
Verifies that the actual map does not contain any of the given keys.Examples:
Map<Ring, TolkienCharacter> elvesRingBearers = new HashMap<>(); elvesRingBearers.put(nenya, galadriel); elvesRingBearers.put(narya, gandalf); elvesRingBearers.put(vilya, elrond); // assertion will pass assertThat(elvesRingBearers).doesNotContainKeys(oneRing, someManRing); // assertions will fail assertThat(elvesRingBearers).doesNotContainKeys(vilya, nenya); assertThat(elvesRingBearers).doesNotContainKeys(vilya, oneRing);
- Parameters:
keys
- the given keys- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map contains the given key.
-
containsOnlyKeys
@SafeVarargs public final SELF containsOnlyKeys(K... keys)
Verifies that the actual map contains only the given keys and nothing else, in any order.The verification tries to honor the key comparison semantic of the underlying map implementation. The map under test has to be cloned to identify unexpected elements, but depending on the map implementation this may not always be possible. In case it is not possible, a regular map is used and the key comparison strategy may not be the same as the map under test.
Examples :
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertion will pass assertThat(ringBearers).containsOnlyKeys(oneRing, nenya, narya, vilya); // assertion will fail assertThat(ringBearers).containsOnlyKeys(oneRing, nenya);
- Parameters:
keys
- the given keys that should be in the actual map.- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map does not contain the given keys, i.e. the actual map contains some or none of the given keys, or the actual map contains more entries than the given ones.java.lang.IllegalArgumentException
- if the given argument is an empty array.
-
containsOnlyKeys
public SELF containsOnlyKeys(java.lang.Iterable<? extends K> keys)
Verifies that the actual map contains only the given keys and nothing else, in any order.The verification tries to honor the key comparison semantic of the underlying map implementation. The map under test has to be cloned to identify unexpected elements, but depending on the map implementation this may not always be possible. In case it is not possible, a regular map is used and the key comparison strategy may not be the same as the map under test.
Examples :
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertion will pass assertThat(ringBearers).containsOnlyKeys(Arrays.asList(oneRing, nenya, narya, vilya)); // assertions will fail assertThat(ringBearers).containsOnlyKeys(Arrays.asList(oneRing, nenya)); assertThat(ringBearers).containsOnlyKeys(Arrays.asList(oneRing, nenya, narya, vilya, nibelungRing));
- Parameters:
keys
- the given keys that should be in the actual map.- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
or empty.java.lang.AssertionError
- if the actual map does not contain the given keys, i.e. the actual map contains some or none of the given keys, or the actual map's keys contains keys not in the given ones.java.lang.IllegalArgumentException
- if the given argument is an empty array.- Since:
- 3.12.0
-
containsValue
public SELF containsValue(V value)
Verifies that the actual map contains the given value.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertion will pass assertThat(ringBearers).containsValue(frodo); // assertion will fail assertThat(ringBearers).containsValue(sauron);
- Parameters:
value
- the value to look for.- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map does not contain the given value.
-
containsValues
@SafeVarargs public final SELF containsValues(V... values)
Verifies that the actual map contains the given values.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertion will pass assertThat(ringBearers).containsValues(frodo, galadriel); // assertions will fail assertThat(ringBearers).containsValues(sauron, aragorn); assertThat(ringBearers).containsValues(sauron, frodo);
- Parameters:
values
- the values to look for in the actual map.- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map does not contain the given values.
-
doesNotContainValue
public SELF doesNotContainValue(V value)
Verifies that the actual map does not contain the given value.Examples:
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertion will pass assertThat(ringBearers).doesNotContainValue(aragorn); // assertion will fail assertThat(ringBearers).doesNotContainValue(frodo);
- Parameters:
value
- the value that should not be in actual map.- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.AssertionError
- if the actual map contains the given value.
-
containsOnly
@SafeVarargs public final SELF containsOnly(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map contains only the given entries and nothing else, in any order.The verification tries to honor the key comparison semantic of the underlying map implementation. The map under test has to be cloned to identify unexpected elements, but depending on the map implementation this may not always be possible. In case it is not possible, a regular map is used and the key comparison strategy may not be the same as the map under test.
Examples :
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>(); ringBearers.put(nenya, galadriel); ringBearers.put(narya, gandalf); ringBearers.put(vilya, elrond); ringBearers.put(oneRing, frodo); // assertions will pass assertThat(ringBearers).containsOnly(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf), entry(vilya, elrond)); assertThat(Collections.emptyMap()).containsOnly(); // assertion will fail assertThat(ringBearers).containsOnly(entry(oneRing, frodo), entry(nenya, galadriel));
- Parameters:
entries
- the entries that should be in the actual map.- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actual map isnull
.java.lang.NullPointerException
- if the given argument isnull
.java.lang.IllegalArgumentException
- if the given argument is an empty array.java.lang.AssertionError
- if the actual map does not contain the given entries, i.e. the actual map contains some or none of the given entries, or the actual map contains more entries than the given ones.
-
containsOnlyForProxy
protected SELF containsOnlyForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
-
containsExactly
@SafeVarargs public final SELF containsExactly(java.util.Map.Entry<? extends K,? extends V>... entries)
Verifies that the actual map contains only the given entries and nothing else, in order.
This assertion should only be used with maps that have a consistent iteration order (i.e. don't use it withHashMap
, prefercontainsOnly(java.util.Map.Entry...)
in that case).Examples:
// newLinkedHashMap builds a Map with iteration order corresponding to the insertion order Map<Ring, TolkienCharacter> ringBearers = newLinkedHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf)); // assertions will pass assertThat(ringBearers).containsExactly(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf)); assertThat(Collections.emptyMap()).containsExactly(); // assertion will fail as actual and expected order differ assertThat(ringBearers).containsExactly(entry(nenya, galadriel), entry(narya, gandalf), entry(oneRing, frodo));
- Parameters:
entries
- the given entries.- Returns:
this
assertions object- Throws:
java.lang.NullPointerException
- if the given entries array isnull
.java.lang.AssertionError
- if the actual map isnull
.java.lang.IllegalArgumentException
- if the given entries array is empty.java.lang.AssertionError
- if the actual map does not contain the given entries with same order, i.e. the actual map contains some or none of the given entries, or the actual map contains more entries than the given ones or entries are the same but the order is not.
-
containsExactlyForProxy
protected SELF containsExactlyForProxy(java.util.Map.Entry<? extends K,? extends V>[] entries)
-
isUnmodifiable
public SELF isUnmodifiable()
Verifies that the actual map is unmodifiable, i.e. throws anUnsupportedOperationException
with any attempt to modify the map.Example:
// assertions succeeds assertThat(Collections.unmodifiableMap(new HashMap<>())).isUnmodifiable(); // assertions fails assertThat(new HashMap<>()).isUnmodifiable();
- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actual collection is modifiable.- Since:
- 3.24.0
- See Also:
Collections.unmodifiableMap(Map)
-
assertIsUnmodifiable
private void assertIsUnmodifiable()
-
expectUnsupportedOperationException
private void expectUnsupportedOperationException(java.lang.Runnable runnable, java.lang.String method)
-
usingElementComparator
@Deprecated public SELF usingElementComparator(java.util.Comparator<? super java.util.Map.Entry<? extends K,? extends V>> customComparator)
Deprecated.Custom element Comparator is not supported for MapEntry comparison.Do not use this method.- Specified by:
usingElementComparator
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
customComparator
- the comparator to use for incoming assertion checks.- Returns:
this
assertion object.- Throws:
java.lang.UnsupportedOperationException
- if this method is called.
-
usingDefaultElementComparator
@Deprecated public SELF usingDefaultElementComparator()
Deprecated.Custom element Comparator is not supported for MapEntry comparison.Do not use this method.- Specified by:
usingDefaultElementComparator
in interfaceEnumerableAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Returns:
this
assertion object.- Throws:
java.lang.UnsupportedOperationException
- if this method is called.
-
as
public SELF as(java.lang.String description, java.lang.Object... args)
Description copied from interface:Descriptable
Sets the description of the assertion that is going to be called after.You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
The description follows
String.format(String, Object...)
syntax.Example :
try { // set an incorrect age to Mr Frodo which is really 33 years old. frodo.setAge(50); // specify a test description (call as() before the assertion !), it supports String format syntax. assertThat(frodo.getAge()).as("check %s's age", frodo.getName()).isEqualTo(33); } catch (AssertionError e) { assertThat(e).hasMessage("[check Frodo's age]\n expected: 33\n but was: 50"); }
- Specified by:
as
in interfaceDescriptable<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>>
- Overrides:
as
in classAbstractObjectAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
description
- the new description to set.args
- optional parameter if description is a format String.- Returns:
this
object.- See Also:
Descriptable.describedAs(String, Object...)
-
as
public SELF as(Description description)
Description copied from interface:Descriptable
Sets the description of the assertion that is going to be called after.You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
This overloaded version of "describedAs" offers more flexibility than the one taking a
String
by allowing users to pass their own implementation of a description. For example, a description that creates its value lazily, only when an assertion failure occurs.- Specified by:
as
in interfaceDescriptable<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>>
- Overrides:
as
in classAbstractObjectAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
description
- the new description to set.- Returns:
this
object.- See Also:
Descriptable.describedAs(Description)
-
describedAs
public SELF describedAs(Description description)
Description copied from class:AbstractAssert
Sets the description of the assertion that is going to be called after.You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
This overloaded version of "describedAs" offers more flexibility than the one taking a
String
by allowing users to pass their own implementation of a description. For example, a description that creates its value lazily, only when an assertion failure occurs.- Specified by:
describedAs
in interfaceDescriptable<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>>
- Overrides:
describedAs
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
description
- the new description to set.- Returns:
this
object.
-
describedAs
public SELF describedAs(java.lang.String description, java.lang.Object... args)
Description copied from interface:Descriptable
Sets the description of the assertion that is going to be called after.You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
Alias for
since "as" is a keyword in Groovy.Descriptable.as(String, Object...)
- Specified by:
describedAs
in interfaceDescriptable<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>>
- Parameters:
description
- the new description to set.args
- optional parameter if description is a format String.- Returns:
this
object.
-
doesNotHave
public SELF doesNotHave(Condition<? super ACTUAL> condition)
Description copied from class:AbstractAssert
Verifies that the actual value does not satisfy the given condition. This method is an alias for
.ExtensionPoints.isNot(Condition)
- Specified by:
doesNotHave
in interfaceExtensionPoints<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
doesNotHave
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
condition
- the given condition.- Returns:
this ExtensionPoints
object.- See Also:
ExtensionPoints.isNot(Condition)
-
doesNotHaveSameClassAs
public SELF doesNotHaveSameClassAs(java.lang.Object other)
Description copied from class:AbstractAssert
Verifies that the actual value does not have the same class as the given object.Example:
// assertions succeed assertThat(1).doesNotHaveSameClassAs("abc"); assertThat(new ArrayList<String>()).doesNotHaveSameClassAs(new LinkedList<String>()); // assertions fail assertThat(1).doesNotHaveSameClassAs(2); assertThat("abc").doesNotHaveSameClassAs("123"); assertThat(new ArrayList<String>()).doesNotHaveSameClassAs(new ArrayList<Integer>());
- Specified by:
doesNotHaveSameClassAs
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
doesNotHaveSameClassAs
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
other
- the object to check type against.- Returns:
- this assertion object.
-
has
public SELF has(Condition<? super ACTUAL> condition)
Description copied from class:AbstractAssert
Verifies that the actual value satisfies the given condition. This method is an alias for
.ExtensionPoints.is(Condition)
- Specified by:
has
in interfaceExtensionPoints<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
has
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
condition
- the given condition.- Returns:
this ExtensionPoints
object.- See Also:
ExtensionPoints.is(Condition)
-
hasSameClassAs
public SELF hasSameClassAs(java.lang.Object other)
Description copied from class:AbstractAssert
Verifies that the actual value has the same class as the given object.Example:
// assertions succeed assertThat(1).hasSameClassAs(2); assertThat("abc").hasSameClassAs("123"); assertThat(new ArrayList<String>()).hasSameClassAs(new ArrayList<Integer>()); // assertions fail assertThat(1).hasSameClassAs("abc"); assertThat(new ArrayList<String>()).hasSameClassAs(new LinkedList<String>());
- Specified by:
hasSameClassAs
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
hasSameClassAs
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
other
- the object to check type against.- Returns:
- this assertion object.
-
hasToString
public SELF hasToString(java.lang.String expectedToString)
Description copied from class:AbstractAssert
Verifies that actualactual.toString()
is equal to the givenString
.Example :
CartoonCharacter homer = new CartoonCharacter("Homer"); // Instead of writing ... assertThat(homer.toString()).isEqualTo("Homer"); // ... you can simply write: assertThat(homer).hasToString("Homer");
- Specified by:
hasToString
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
hasToString
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
expectedToString
- the expected String description of actual.- Returns:
- this assertion object.
-
is
public SELF is(Condition<? super ACTUAL> condition)
Description copied from class:AbstractAssert
Verifies that the actual value satisfies the given condition. This method is an alias for
.ExtensionPoints.has(Condition)
- Specified by:
is
in interfaceExtensionPoints<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
is
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
condition
- the given condition.- Returns:
this ExtensionPoints
object.- See Also:
ExtensionPoints.has(Condition)
-
isEqualTo
public SELF isEqualTo(java.lang.Object expected)
Description copied from class:AbstractAssert
Verifies that the actual value is equal to the given one.Example:
// assertions succeed assertThat("abc").isEqualTo("abc"); assertThat(new HashMap<String, Integer>()).isEqualTo(new HashMap<String, Integer>()); // assertions fail assertThat("abc").isEqualTo("123"); assertThat(new ArrayList<String>()).isEqualTo(1);
- Specified by:
isEqualTo
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isEqualTo
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
expected
- the given value to compare the actual value to.- Returns:
this
assertion object.
-
isExactlyInstanceOf
public SELF isExactlyInstanceOf(java.lang.Class<?> type)
Description copied from class:AbstractAssert
Verifies that the actual value is exactly an instance of the given type.Example:
// assertions succeed assertThat("abc").isExactlyInstanceOf(String.class); assertThat(new ArrayList<String>()).isExactlyInstanceOf(ArrayList.class); assertThat(new HashMap<String, Integer>()).isExactlyInstanceOf(HashMap.class); // assertions fail assertThat(1).isExactlyInstanceOf(String.class); assertThat(new ArrayList<String>()).isExactlyInstanceOf(List.class); assertThat(new HashMap<String, Integer>()).isExactlyInstanceOf(Map.class);
- Specified by:
isExactlyInstanceOf
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isExactlyInstanceOf
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
type
- the type to check the actual value against.- Returns:
- this assertion object.
-
isIn
public SELF isIn(java.lang.Iterable<?> values)
Description copied from class:AbstractAssert
Verifies that the actual value is present in the given iterable.This assertion always fails if the given iterable is empty.
Example:
Iterable<Ring> elvesRings = list(vilya, nenya, narya); // assertion succeeds assertThat(nenya).isIn(elvesRings); // assertions fail: assertThat(oneRing).isIn(elvesRings); assertThat(oneRing).isIn(emptyList());
- Specified by:
isIn
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isIn
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
values
- the given iterable to search the actual value in.- Returns:
this
assertion object.
-
isIn
public SELF isIn(java.lang.Object... values)
Description copied from class:AbstractAssert
Verifies that the actual value is present in the given array of values.This assertion always fails if the given array of values is empty.
Example:
Ring[] elvesRings = new Ring[] { vilya, nenya, narya }; // assertion succeeds assertThat(nenya).isIn(elvesRings); // assertions fail assertThat(oneRing).isIn(elvesRings); assertThat(oneRing).isIn(new Ring[0]);
- Specified by:
isIn
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isIn
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
values
- the given array to search the actual value in.- Returns:
this
assertion object.
-
isInstanceOf
public SELF isInstanceOf(java.lang.Class<?> type)
Description copied from class:AbstractAssert
Verifies that the actual value is an instance of the given type.Example:
// assertions succeed assertThat("abc").isInstanceOf(String.class); assertThat(new HashMap<String, Integer>()).isInstanceOf(HashMap.class); assertThat(new HashMap<String, Integer>()).isInstanceOf(Map.class); // assertions fail assertThat(1).isInstanceOf(String.class); assertThat(new ArrayList<String>()).isInstanceOf(LinkedList.class);
- Specified by:
isInstanceOf
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isInstanceOf
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
type
- the type to check the actual value against.- Returns:
- this assertion object.
-
isInstanceOfAny
public SELF isInstanceOfAny(java.lang.Class<?>... types)
Description copied from class:AbstractAssert
Verifies that the actual value is an instance of any of the given types.Example:
// assertions succeed assertThat("abc").isInstanceOfAny(String.class, Integer.class); assertThat(new ArrayList<String>()).isInstanceOfAny(LinkedList.class, ArrayList.class); assertThat(new HashMap<String, Integer>()).isInstanceOfAny(TreeMap.class, Map.class); // assertions fail assertThat(1).isInstanceOfAny(Double.class, Float.class); assertThat(new ArrayList<String>()).isInstanceOfAny(LinkedList.class, Vector.class);
- Specified by:
isInstanceOfAny
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isInstanceOfAny
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
types
- the types to check the actual value against.- Returns:
- this assertion object.
-
isNot
public SELF isNot(Condition<? super ACTUAL> condition)
Description copied from class:AbstractAssert
Verifies that the actual value does not satisfy the given condition. This method is an alias for
.ExtensionPoints.doesNotHave(Condition)
- Specified by:
isNot
in interfaceExtensionPoints<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isNot
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
condition
- the given condition.- Returns:
this ExtensionPoints
object.- See Also:
ExtensionPoints.isNot(Condition)
-
isNotEqualTo
public SELF isNotEqualTo(java.lang.Object other)
Description copied from class:AbstractAssert
Verifies that the actual value is not equal to the given one.Example:
// assertions succeed assertThat("abc").isNotEqualTo("123"); assertThat(new ArrayList<String>()).isNotEqualTo(1); // assertions fail assertThat("abc").isNotEqualTo("abc"); assertThat(new HashMap<String, Integer>()).isNotEqualTo(new HashMap<String, Integer>());
- Specified by:
isNotEqualTo
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isNotEqualTo
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.
-
isNotExactlyInstanceOf
public SELF isNotExactlyInstanceOf(java.lang.Class<?> type)
Description copied from class:AbstractAssert
Verifies that the actual value is not exactly an instance of given type.Example:
// assertions succeed assertThat(1).isNotExactlyInstanceOf(String.class); assertThat(new ArrayList<String>()).isNotExactlyInstanceOf(List.class); assertThat(new HashMap<String, Integer>()).isNotExactlyInstanceOf(Map.class); // assertions fail assertThat("abc").isNotExactlyInstanceOf(String.class); assertThat(new ArrayList<String>()).isNotExactlyInstanceOf(ArrayList.class); assertThat(new HashMap<String, Integer>()).isNotExactlyInstanceOf(HashMap.class);
- Specified by:
isNotExactlyInstanceOf
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isNotExactlyInstanceOf
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
type
- the type to check the actual value against.- Returns:
- this assertion object.
-
isNotIn
public SELF isNotIn(java.lang.Iterable<?> values)
Description copied from class:AbstractAssert
Verifies that the actual value is not present in the given iterable.This assertion always succeeds if the given iterable is empty.
Example:
Iterable<Ring> elvesRings = list(vilya, nenya, narya); // assertions succeed: assertThat(oneRing).isNotIn(elvesRings); assertThat(oneRing).isNotIn(emptyList()); // assertions fails: assertThat(nenya).isNotIn(elvesRings);
- Specified by:
isNotIn
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isNotIn
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
values
- the given iterable to search the actual value in.- Returns:
this
assertion object.
-
isNotIn
public SELF isNotIn(java.lang.Object... values)
Description copied from class:AbstractAssert
Verifies that the actual value is not present in the given array of values.This assertion always succeeds if the given array of values is empty.
Example:
Ring[] elvesRings = new Ring[] { vilya, nenya, narya }; // assertions succeed assertThat(oneRing).isNotIn(elvesRings); assertThat(oneRing).isNotIn(new Ring[0]); // assertions fails: assertThat(nenya).isNotIn(elvesRings);
- Specified by:
isNotIn
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isNotIn
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
values
- the given array to search the actual value in.- Returns:
this
assertion object.
-
isNotInstanceOf
public SELF isNotInstanceOf(java.lang.Class<?> type)
Description copied from class:AbstractAssert
Verifies that the actual value is not an instance of the given type.Example:
// assertions succeed assertThat(1).isNotInstanceOf(Double.class); assertThat(new ArrayList<String>()).isNotInstanceOf(LinkedList.class); // assertions fail assertThat("abc").isNotInstanceOf(String.class); assertThat(new HashMap<String, Integer>()).isNotInstanceOf(HashMap.class); assertThat(new HashMap<String, Integer>()).isNotInstanceOf(Map.class);
- Specified by:
isNotInstanceOf
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isNotInstanceOf
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
type
- the type to check the actual value against.- Returns:
- this assertion object.
-
isNotInstanceOfAny
public SELF isNotInstanceOfAny(java.lang.Class<?>... types)
Description copied from class:AbstractAssert
Verifies that the actual value is not an instance of any of the given types.Example:
// assertions succeed assertThat(1).isNotInstanceOfAny(Double.class, Float.class); assertThat(new ArrayList<String>()).isNotInstanceOfAny(LinkedList.class, Vector.class); // assertions fail assertThat(1).isNotInstanceOfAny(Double.class, Integer.class); assertThat(new ArrayList<String>()).isNotInstanceOfAny(LinkedList.class, ArrayList.class); assertThat(new HashMap<String, Integer>()).isNotInstanceOfAny(TreeMap.class, Map.class);
- Specified by:
isNotInstanceOfAny
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isNotInstanceOfAny
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
types
- the types to check the actual value against.- Returns:
- this assertion object.
-
isNotOfAnyClassIn
public SELF isNotOfAnyClassIn(java.lang.Class<?>... types)
Description copied from class:AbstractAssert
Verifies that the actual value type is not in given types.Example:
// assertions succeed assertThat(new HashMap<String, Integer>()).isNotOfAnyClassIn(Map.class, TreeMap.class); assertThat(new ArrayList<String>()).isNotOfAnyClassIn(LinkedList.class, List.class); // assertions fail assertThat(new HashMap<String, Integer>()).isNotOfAnyClassIn(HashMap.class, TreeMap.class); assertThat(new ArrayList<String>()).isNotOfAnyClassIn(ArrayList.class, LinkedList.class);
- Specified by:
isNotOfAnyClassIn
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isNotOfAnyClassIn
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
types
- the types to check the actual value against.- Returns:
- this assertion object.
-
isNotNull
public SELF isNotNull()
Description copied from class:AbstractAssert
Verifies that the actual value is notnull
.Example:
// assertions succeed assertThat("abc").isNotNull(); assertThat(new HashMap<String, Integer>()).isNotNull(); // assertions fails String value = null; assertThat(value).isNotNull();
-
isNotSameAs
public SELF isNotSameAs(java.lang.Object other)
Description copied from class:AbstractAssert
Verifies that the actual value is not the same as the given one, ie using == comparison.Example:
// Name is a class with first and last fields, two Names are equals if both first and last are equals. Name tyrion = new Name("Tyrion", "Lannister"); Name alias = tyrion; Name clone = new Name("Tyrion", "Lannister"); // assertions succeed: assertThat(clone).isNotSameAs(tyrion) .isEqualTo(tyrion); // assertion fails: assertThat(alias).isNotSameAs(tyrion);
- Specified by:
isNotSameAs
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isNotSameAs
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.
-
isOfAnyClassIn
public SELF isOfAnyClassIn(java.lang.Class<?>... types)
Description copied from class:AbstractAssert
Verifies that the actual value type is in given types.Example:
// assertions succeed assertThat(new HashMap<String, Integer>()).isOfAnyClassIn(HashMap.class, TreeMap.class); assertThat(new ArrayList<String>()).isOfAnyClassIn(ArrayList.class, LinkedList.class); // assertions fail assertThat(new HashMap<String, Integer>()).isOfAnyClassIn(TreeMap.class, Map.class); assertThat(new ArrayList<String>()).isOfAnyClassIn(LinkedList.class, List.class);
- Specified by:
isOfAnyClassIn
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isOfAnyClassIn
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
types
- the types to check the actual value against.- Returns:
- this assertion object.
-
isSameAs
public SELF isSameAs(java.lang.Object expected)
Description copied from class:AbstractAssert
Verifies that the actual value is the same as the given one, ie using == comparison.Example:
// Name is a class with first and last fields, two Names are equals if both first and last are equals. Name tyrion = new Name("Tyrion", "Lannister"); Name alias = tyrion; Name clone = new Name("Tyrion", "Lannister"); // assertions succeed: assertThat(tyrion).isSameAs(alias) .isEqualTo(clone); // assertion fails: assertThat(tyrion).isSameAs(clone);
- Specified by:
isSameAs
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
isSameAs
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
expected
- the given value to compare the actual value to.- Returns:
this
assertion object.
-
overridingErrorMessage
public SELF overridingErrorMessage(java.lang.String newErrorMessage, java.lang.Object... args)
Description copied from class:AbstractAssert
Overrides AssertJ default error message by the given one.You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
The new error message is built using
String.format(String, Object...)
if you provide args parameter (if you don't, the error message is taken as it is).Example :
assertThat(player.isRookie()).overridingErrorMessage("Expecting Player <%s> to be a rookie but was not.", player) .isTrue();
- Overrides:
overridingErrorMessage
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
newErrorMessage
- the error message that will replace the default one provided by Assertj.args
- the args used to fill error message as inString.format(String, Object...)
.- Returns:
- this assertion object.
-
usingDefaultComparator
public SELF usingDefaultComparator()
Description copied from class:AbstractAssert
Revert to standard comparison for the incoming assertion checks.This method should be used to disable a custom comparison strategy set by calling
usingComparator
.- Specified by:
usingDefaultComparator
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
usingDefaultComparator
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Returns:
this
assertion object.
-
usingComparator
public SELF usingComparator(java.util.Comparator<? super ACTUAL> customComparator)
Description copied from class:AbstractAssert
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.The custom comparator is bound to assertion instance, meaning that if a new assertion instance is created, the default comparison strategy will be used.
Examples :
// frodo and sam are instances of Character with Hobbit race (obviously :). // raceComparator implements Comparator<Character> assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam);
- Specified by:
usingComparator
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
usingComparator
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
customComparator
- the comparator to use for the incoming assertion checks.- Returns:
this
assertion object.
-
usingComparator
public SELF usingComparator(java.util.Comparator<? super ACTUAL> customComparator, java.lang.String customComparatorDescription)
Description copied from class:AbstractAssert
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.The custom comparator is bound to assertion instance, meaning that if a new assertion instance is created, the default comparison strategy will be used.
Examples :
// frodo and sam are instances of Character with Hobbit race (obviously :). // raceComparator implements Comparator<Character> assertThat(frodo).usingComparator(raceComparator, "Hobbit Race Comparator").isEqualTo(sam);
- Specified by:
usingComparator
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
usingComparator
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
customComparator
- the comparator to use for the incoming assertion checks.customComparatorDescription
- comparator description to be used in assertion error messages- Returns:
this
assertion object.
-
withFailMessage
public SELF withFailMessage(java.lang.String newErrorMessage, java.lang.Object... args)
Description copied from class:AbstractAssert
Alternative method forAbstractAssert.overridingErrorMessage(java.lang.String, java.lang.Object...)
You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
Example:
assertThat(player.isRookie()).withFailMessage("Expecting Player <%s> to be a rookie but was not.", player) .isTrue();
- Overrides:
withFailMessage
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
newErrorMessage
- the error message that will replace the default one provided by Assertj.args
- the args used to fill error message as inString.format(String, Object...)
.- Returns:
- this assertion object.
-
withThreadDumpOnError
public SELF withThreadDumpOnError()
Description copied from class:AbstractAssert
In case of an assertion error, a thread dump will be printed toSystem.err
.Example :
will print a thread dump, something similar to this:assertThat("Messi").withThreadDumpOnError().isEqualTo("Ronaldo");
"JDWP Command Reader" java.lang.Thread.State: RUNNABLE "JDWP Event Helper Thread" java.lang.Thread.State: RUNNABLE "JDWP Transport Listener: dt_socket" java.lang.Thread.State: RUNNABLE "Signal Dispatcher" java.lang.Thread.State: RUNNABLE "Finalizer" java.lang.Thread.State: WAITING at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189) "Reference Handler" java.lang.Thread.State: WAITING at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:503) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133) "main" java.lang.Thread.State: RUNNABLE at sun.management.ThreadImpl.dumpThreads0(Native Method) at sun.management.ThreadImpl.dumpAllThreads(ThreadImpl.java:446) at org.assertj.core.internal.Failures.threadDumpDescription(Failures.java:193) at org.assertj.core.internal.Failures.printThreadDumpIfNeeded(Failures.java:141) at org.assertj.core.internal.Failures.failure(Failures.java:91) at org.assertj.core.internal.Objects.assertEqual(Objects.java:314) at org.assertj.core.api.AbstractAssert.isEqualTo(AbstractAssert.java:198) at org.assertj.examples.ThreadDumpOnErrorExample.main(ThreadDumpOnErrorExample.java:28)
- Specified by:
withThreadDumpOnError
in interfaceAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Overrides:
withThreadDumpOnError
in classAbstractAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Returns:
- this assertion object.
-
size
public AbstractMapSizeAssert<SELF,ACTUAL,K,V> size()
Returns anAssert
object that allows performing assertions on the size of theMap
under test.Once this method is called, the object under test is no longer the
Map
but its size, to perform assertions on theMap
, callAbstractMapSizeAssert.returnToMap()
.Examples:
Map<Ring, TolkienCharacter> ringBearers = newHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf)); // assertion will pass: assertThat(ringBearers).size().isGreaterThan(1) .isLessThanOrEqualTo(3) returnToMap().contains(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf)); // assertion will fail: assertThat(ringBearers).size().isGreaterThan(5);
- Returns:
- a
AbstractMapSizeAssert
to allow assertions on the number of key-value mappings in this map - Throws:
java.lang.NullPointerException
- if the given map isnull
.
-
extracting
@Deprecated public AbstractListAssert<?,java.util.List<?>,java.lang.Object,ObjectAssert<java.lang.Object>> extracting(java.lang.Object... keys)
Deprecated.useextractingByKeys(Object[])
insteadExtract the values of given keys from the map under test into an array, this new array becoming the object under test.For example, if you specify "id", "name" and "email" keys then the array will contain the map values for these keys, you can then perform array assertions on the extracted values.
If a given key is not present in the map under test, a null value is extracted.
Example:
Map<String, Object> map = new HashMap<>(); map.put("name", "kawhi"); map.put("age", 25); assertThat(map).extracting("name", "age") .contains("kawhi", 25);
Note that the order of extracted keys value is consistent with the iteration order of the array under test.
Nested keys are not yet supported, passing "name.first" won't get a value for "name" and then try to extract "first" from the previously extracted value, instead it will simply look for a value under "name.first" key.
- Parameters:
keys
- the keys used to get values from the map under test- Returns:
- a new assertion object whose object under test is the array containing the extracted map values
-
extractingByKeys
@SafeVarargs public final AbstractListAssert<?,java.util.List<? extends V>,V,ObjectAssert<V>> extractingByKeys(K... keys)
Extract the values of given keys from the map under test into an array, this new array becoming the object under test.For example, if you specify "id", "name" and "email" keys then the array will contain the map values for these keys, you can then perform array assertions on the extracted values.
If a given key is not present in the map under test, a null value is extracted.
Example:
Map<String, Object> map = new HashMap<>(); map.put("name", "kawhi"); map.put("age", 25); assertThat(map).extractingByKeys("name", "age") .contains("kawhi", 25);
Note that:
- The order of the extracted key values is consistent with the iteration order of given keys.
- Providing no keys will result in an empty list.
- Parameters:
keys
- the keys used to get values from the map under test- Returns:
- a new assertion object whose object under test is the array containing the extracted map values
- Since:
- 3.14.0
-
extractingByKeysForProxy
protected AbstractListAssert<?,java.util.List<? extends V>,V,ObjectAssert<V>> extractingByKeysForProxy(K[] keys)
-
extracting
@Deprecated public AbstractObjectAssert<?,?> extracting(java.lang.Object key)
Deprecated.useextractingByKey(Object)
insteadExtract the value of given key from the map under test, the extracted value becoming the new object under test.For example, if you specify "id" key, then the object under test will be the map value for this key.
If a given key is not present in the map under test, a null value is extracted.
Example:
Map<String, Object> map = new HashMap<>(); map.put("name", "kawhi"); assertThat(map).extracting("name") .isEqualTo("kawhi");
Nested keys are not yet supported, passing "name.first" won't get a value for "name" and then try to extract "first" from the previously extracted value, instead it will simply look for a value under "name.first" key.
- Parameters:
key
- the key used to get value from the map under test- Returns:
- a new
ObjectAssert
instance whose object under test is the extracted map value - Since:
- 3.13.0
-
extractingByKey
public AbstractObjectAssert<?,V> extractingByKey(K key)
Extract the value of given key from the map under test, the extracted value becoming the new object under test.For example, if you specify "id" key, then the object under test will be the map value for this key.
If a given key is not present in the map under test, a null value is extracted.
Example:
Map<String, Object> map = new HashMap<>(); map.put("name", "kawhi"); assertThat(map).extractingByKey("name") .isEqualTo("kawhi");
- Parameters:
key
- the key used to get value from the map under test- Returns:
- a new
ObjectAssert
instance whose object under test is the extracted map value - Since:
- 3.14.0
- See Also:
extractingByKey(Object, InstanceOfAssertFactory)
-
extractingByKey
public <ASSERT extends AbstractAssert<?,?>> ASSERT extractingByKey(K key, InstanceOfAssertFactory<?,ASSERT> assertFactory)
Extract the value of given key from the map under test, the extracted value becoming the new object under test.For example, if you specify "id" key, then the object under test will be the map value for this key.
If a given key is not present in the map under test, the assertion will fail.
The
assertFactory
parameter allows to specify anInstanceOfAssertFactory
, which is used to get the assertions narrowed to the factory type.Wrapping the given
InstanceOfAssertFactory
withAssertions.as(InstanceOfAssertFactory)
makes the assertion more readable.Example:
Map<String, Object> map = new HashMap<>(); map.put("name", "kawhi"); assertThat(map).extractingByKey("name", as(InstanceOfAssertFactories.STRING)) .startsWith("kaw");
Nested keys are not yet supported, passing "name.first" won't get a value for "name" and then try to extract "first" from the previously extracted value, instead it will simply look for a value under "name.first" key.
- Type Parameters:
ASSERT
- the type of the resultingAssert
- Parameters:
key
- the key used to get value from the map under testassertFactory
- the factory which verifies the type and creates the newAssert
- Returns:
- a new narrowed
Assert
instance whose object under test is the extracted map value - Throws:
java.lang.NullPointerException
- if the given factory isnull
- Since:
- 3.14.0
-
internalExtractingByKey
private AbstractObjectAssert<?,V> internalExtractingByKey(K key)
-
extractingFromEntries
public AbstractListAssert<?,java.util.List<?>,java.lang.Object,ObjectAssert<java.lang.Object>> extractingFromEntries(java.util.function.Function<? super java.util.Map.Entry<K,V>,java.lang.Object> extractor)
Use the givenFunction
to extract a value from theMap
's entries. The extracted values are stored in a new list becoming the object under test.Let's take a look at an example to make things clearer :
// Build a Map that associates family roles and name of the Simpson family Map<String, CartoonCharacter> characters = new HashMap<>(); characters.put("dad", new CartoonCharacter("Homer")); characters.put("mom", new CartoonCharacter("Marge")); characters.put("girl", new CartoonCharacter("Lisa")); characters.put("boy", new CartoonCharacter("Bart")); assertThat(characters).extractingFromEntries(e -> e.getValue().getName()) .containsOnly("Homer", "Marge", "Lisa", "Bart");
- Parameters:
extractor
- the extractor function to extract a value from an entry of the Map under test.- Returns:
- a new assertion object whose object under test is the list of values extracted
- Since:
- 3.12.0
-
extractingFromEntries
@SafeVarargs public final AbstractListAssert<?,java.util.List<? extends Tuple>,Tuple,ObjectAssert<Tuple>> extractingFromEntries(java.util.function.Function<? super java.util.Map.Entry<K,V>,java.lang.Object>... extractors)
Use the givenFunction
s to extract values from theMap
's entries. The extracted values are stored in a new list composed ofTuple
s (a simple data structure containing the extracted values), this new list becoming the object under test.This method works as
extractingFromEntries(java.util.function.Function)
except that it is designed to extract multiple values from theMap
entries. That's why here the new object under test is a List ofTuple
s.The Tuple data corresponds to the extracted values from the Map's entries, for instance if you pass functions extracting the "id", "name" and "email" values then each Tuple data will be composed of an id, a name and an email extracted from the entry of the Map (the Tuple's data order is the same as the given functions order).
Let's take a look at an example to make things clearer :
Note that the order of extracted property/field values is consistent with the iteration order of the Iterable under test, for example if it's a// Build a Map that associates family roles and name of the Simpson family Map<String, CartoonCharacter> characters = new HashMap<>(); characters.put("dad", new CartoonCharacter("Homer")); characters.put("mom", new CartoonCharacter("Marge")); characters.put("girl", new CartoonCharacter("Lisa")); characters.put("boy", new CartoonCharacter("Bart")); assertThat(characters).extractingFromEntries(e -> e.getKey(), e -> e.getValue().getName()) .containsOnly(tuple("dad", "Homer"), tuple("mom", "Marge"), tuple("girl", "Lisa"), tuple("boy", "Bart"));
HashSet
, you won't be able to make any assumptions on the extracted values order.- Parameters:
extractors
- the extractor functions to extract values from an entry of the Map under test.- Returns:
- a new assertion object whose object under test is the list of Tuples containing the extracted values.
- Since:
- 3.12.0
-
extractingFromEntriesForProxy
protected AbstractListAssert<?,java.util.List<? extends Tuple>,Tuple,ObjectAssert<Tuple>> extractingFromEntriesForProxy(java.util.function.Function<? super java.util.Map.Entry<K,V>,java.lang.Object>[] extractors)
-
flatExtracting
public AbstractListAssert<?,java.util.List<?>,java.lang.Object,ObjectAssert<java.lang.Object>> flatExtracting(java.lang.String... keys)
Flatten the values of the given keys from the actual map under test into a new array, this new array becoming the object under test.If a given key is not present in the map under test, a
null
value is extracted.If a given key value is not an
Iterable
or an array, it is simply extracted but (obviously) not flattened.Example:
List<String> names = asList("Dave", "Jeff"); LinkedHashSet<String> jobs = newLinkedHashSet("Plumber", "Builder"); Iterable<String> cities = asList("Dover", "Boston", "Paris"); int[] ranks = { 1, 2, 3 }; Map<String, Object> map = new LinkedHashMap<>(); map.put("name", names); map.put("job", jobs); map.put("city", cities); map.put("rank", ranks); assertThat(map).flatExtracting("name","job","city", "rank") .containsExactly("Dave", "Jeff", "Plumber", "Builder", "Dover", "Boston", "Paris", 1, 2, 3); // the order of values in the resulting array is the order of map keys then key values: assertThat(map).flatExtracting("city", "job", "name") .containsExactly("Dover", "Boston", "Paris", "Plumber", "Builder", "Dave", "Jeff"); // contains exactly null twice (one for each unknown keys) assertThat(map).flatExtracting("foo", "name", "bar") .containsExactly(null, "Dave", "Jeff", null); // if the key value is not an iterable/array, it will be simply extracted but not flattened. map.put("year", 2017)); assertThat(map).flatExtracting("name","job","year") .containsExactly("Dave", "Jeff", "Plumber", "Builder", "Dover", 2017);
Note that the order of values in the resulting array is the order of the map keys iteration then key values.
- Parameters:
keys
- the keys used to get values from the map under test- Returns:
- a new assertion object whose object under test is the array containing the extracted flattened map values
-
usingRecursiveComparison
public RecursiveComparisonAssert<?> usingRecursiveComparison()
Enable using a recursive field by field comparison strategy when calling the chainedRecursiveComparisonAssert
,Example:
A detailed documentation for the recursive comparison is available here: https://assertj.github.io/doc/#assertj-core-recursive-comparison.public class Person { String name; boolean hasPhd; } public class Doctor { String name; boolean hasPhd; } Doctor drSheldon = new Doctor("Sheldon Cooper", true); Doctor drLeonard = new Doctor("Leonard Hofstadter", true); Doctor drRaj = new Doctor("Raj Koothrappali", true); Person sheldon = new Person("Sheldon Cooper", true); Person leonard = new Person("Leonard Hofstadter", true); Person raj = new Person("Raj Koothrappali", true); Map<String, Doctor> doctors = mapOf(entry(drSheldon.name, drSheldon), entry(drLeonard.name, drLeonard), entry(drRaj.name, drRaj)); Map<String, Person> people = mapOf(entry(sheldon.name, sheldon), entry(leonard.name, leonard), entry(raj.name, raj)); // assertion succeeds as both maps contains equivalent items. assertThat(doctors).usingRecursiveComparison() .isEqualTo(people); // assertion fails because leonard names are different. leonard.setName("Leonard Ofstater"); assertThat(doctors).usingRecursiveComparison() .isEqualTo(people);
The default recursive comparison behavior is
configured
as follows:- different types of map can be compared by default, this allows to compare for example an
HashMap<Person>
and aLinkedHashMap<PersonDto>
.
This behavior can be turned off by callingwithStrictTypeChecking
. - overridden equals methods are used in the comparison (unless stated otherwise - see https://assertj.github.io/doc/#assertj-core-recursive-comparison-ignoring-equals)
- the following types are compared with these comparators:
java.lang.Double
:DoubleComparator
with precision of 1.0E-15java.lang.Float
:FloatComparator
with precision of 1.0E-6- any comparators previously registered with
AbstractIterableAssert.usingComparatorForType(Comparator, Class)
At the moment, only `isEqualTo` can be chained after this method but there are plans to provide assertions.
- Overrides:
usingRecursiveComparison
in classAbstractObjectAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Returns:
- a new
RecursiveComparisonAssert
instance - See Also:
RecursiveComparisonConfiguration
- different types of map can be compared by default, this allows to compare for example an
-
usingRecursiveComparison
public RecursiveComparisonAssert<?> usingRecursiveComparison(RecursiveComparisonConfiguration recursiveComparisonConfiguration)
Same asusingRecursiveComparison()
but allows to specify your ownRecursiveComparisonConfiguration
.- Overrides:
usingRecursiveComparison
in classAbstractObjectAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
recursiveComparisonConfiguration
- theRecursiveComparisonConfiguration
used in the chainedisEqualTo
assertion.- Returns:
- a new
RecursiveComparisonAssert
instance built with the givenRecursiveComparisonConfiguration
.
-
usingRecursiveAssertion
public RecursiveAssertionAssert usingRecursiveAssertion()
Asserts that the given predicate is met for all fields of the object under test recursively (but not the object itself).
For example if the object under test is an instance of class A, A has a B field and B a C field then the assertion checks A's B field and B's C field and all C's fields.
The recursive algorithm employs cycle detection, so object graphs with cyclic references can safely be asserted over without causing looping.
This method enables recursive asserting using default configuration, which means all fields of all objects have the
Predicate
applied to them (including primitive fields), no fields are excluded, but:- The recursion does not enter into Java Class Library types (java.*, javax.*)
- The
Predicate
is applied toCollection
and array elements (but the collection/array itself) - The
Predicate
is applied toMap
values but not the map itself or its keys - The
Predicate
is applied toOptional
and primitive optional values
It is possible to assert several predicates over the object graph in a row.
The classes used in recursive asserting are not thread safe. Care must be taken when running tests in parallel not to run assertions over object graphs that are being shared between tests.
Example
class Author { String name; String email; List<Book> books = new ArrayList<>(); Author(String name, String email) { this.name = name; this.email = email; } } class Book { String title; Author[] authors; Book(String title, Author[] authors) { this.title = title; this.authors = authors; } } ... Author pramodSadalage = new Author("Pramod Sadalage", "p.sadalage@recursive.test"); Author martinFowler = new Author("Martin Fowler", "m.fowler@recursive.test"); Author kentBeck = new Author("Kent Beck", "k.beck@recursive.test"); Book noSqlDistilled = new Book("NoSql Distilled", new Author[] {pramodSadalage, martinFowler}); pramodSadalage.books.add(noSqlDistilled); martinFowler.books.add(noSqlDistilled); Book refactoring = new Book("Refactoring", new Author[] {martinFowler, kentBeck}); martinFowler.books.add(refactoring); kentBeck.books.add(refactoring); // assertion succeeds Map<String, Author> authors = new HashMap<>(); authors.put("MF", martinFowler); authors.put("KB", kentBeck); assertThat(authors).usingRecursiveAssertion() .allFieldsSatisfy(field -> field != null);
In case one or more fields in the object graph fails the predicate test, the entire assertion will fail. Failing fields will be listed in the failure report using a JSON path-ish notation.
- Overrides:
usingRecursiveAssertion
in classAbstractObjectAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Returns:
- A new instance of
RecursiveAssertionAssert
built with a defaultRecursiveAssertionConfiguration
.
-
usingRecursiveAssertion
public RecursiveAssertionAssert usingRecursiveAssertion(RecursiveAssertionConfiguration recursiveAssertionConfiguration)
The same as
usingRecursiveAssertion()
, but this method allows the developer to pass in an explicit recursion configuration. This configuration gives fine-grained control over what to include in the recursion, such as:- Exclusion of fields that are null
- Exclusion of fields by path
- Exclusion of fields by type
- Exclusion of primitive fields
- Inclusion of Java Class Library types in the recursive execution
- Treatment of
Collection
and array objects - Treatment of
Map
objects - Treatment of Optional and primitive Optional objects
Please refer to the documentation of
RecursiveAssertionConfiguration.Builder
for more details.- Overrides:
usingRecursiveAssertion
in classAbstractObjectAssert<SELF extends AbstractMapAssert<SELF,ACTUAL,K,V>,ACTUAL extends java.util.Map<K,V>>
- Parameters:
recursiveAssertionConfiguration
- The recursion configuration described above.- Returns:
- A new instance of
RecursiveAssertionAssert
built with a defaultRecursiveAssertionConfiguration
.
-
flatten
private static java.util.List<java.lang.Object> flatten(java.lang.Iterable<java.lang.Object> collectionToFlatten)
-
-