Class MapAssert<KEY,VALUE>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<SELF,ACTUAL>
-
- org.assertj.core.api.AbstractObjectAssert<SELF,ACTUAL>
-
- org.assertj.core.api.AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
-
- org.assertj.core.api.MapAssert<KEY,VALUE>
-
- All Implemented Interfaces:
Assert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>>
,Descriptable<MapAssert<KEY,VALUE>>
,EnumerableAssert<MapAssert<KEY,VALUE>,Map.Entry<? extends KEY,? extends VALUE>>
,ExtensionPoints<MapAssert<KEY,VALUE>,Map<KEY,VALUE>>
public class MapAssert<KEY,VALUE> extends AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
Assertions forMap
s.To create a new instance of this class, invoke
.Assertions.assertThat(Map)
- Author:
- David DIDIER, Yvonne Wang, Alex Ruiz, Mikhail Mazursky, Nicolas François
-
-
Field Summary
-
Fields inherited from class org.assertj.core.api.AbstractMapAssert
maps
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, info, myself, objects, throwUnsupportedExceptionOnEquals
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MapAssert<KEY,VALUE>
contains(Map.Entry<? extends KEY,? extends VALUE>... entries)
Verifies that the actual map contains the given entries, in any order.MapAssert<KEY,VALUE>
containsAnyOf(Map.Entry<? extends KEY,? extends VALUE>... entries)
Verifies that the actual map contains at least one of the given entries.MapAssert<KEY,VALUE>
containsExactly(Map.Entry<? extends KEY,? extends VALUE>... 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.MapAssert<KEY,VALUE>
containsKeys(KEY... keys)
Verifies that the actual map contains the given keys.MapAssert<KEY,VALUE>
containsOnly(Map.Entry<? extends KEY,? extends VALUE>... entries)
Verifies that the actual map contains only the given entries and nothing else, in any order.MapAssert<KEY,VALUE>
containsOnlyKeys(KEY... keys)
Verifies that the actual map contains only the given keys and nothing else, in any order.MapAssert<KEY,VALUE>
containsValues(VALUE... values)
Verifies that the actual map contains the given values.MapAssert<KEY,VALUE>
doesNotContain(Map.Entry<? extends KEY,? extends VALUE>... entries)
Verifies that the actual map does not contain the given entries.MapAssert<KEY,VALUE>
doesNotContainKeys(KEY... keys)
Verifies that the actual map does not contain any of the given keys.AbstractListAssert<?,List<?>,Object,ObjectAssert<Object>>
extracting(Function<? super Map<KEY,VALUE>,?>... extractors)
Uses the givenFunction
s to extract the values from the object under test into a list, this new list becoming the object under test.AbstractListAssert<?,List<? extends VALUE>,VALUE,ObjectAssert<VALUE>>
extractingByKeys(KEY... keys)
Extract the values of given keys from the map under test into an array, this new array becoming the object under test.AbstractListAssert<?,List<? extends Tuple>,Tuple,ObjectAssert<Tuple>>
extractingFromEntries(Function<? super Map.Entry<KEY,VALUE>,Object>... extractors)
-
Methods inherited from class org.assertj.core.api.AbstractMapAssert
allSatisfy, anySatisfy, as, as, containsAllEntriesOf, containsEntry, containsExactlyEntriesOf, containsExactlyInAnyOrderEntriesOf, containsKey, containsOnlyKeys, containsValue, describedAs, describedAs, doesNotContainEntry, doesNotContainKey, doesNotContainValue, doesNotHave, doesNotHaveSameClassAs, extracting, extracting, extractingByKey, extractingByKey, extractingFromEntries, flatExtracting, has, hasEntrySatisfying, hasEntrySatisfying, hasEntrySatisfying, hasEntrySatisfying, hasKeySatisfying, hasSameClassAs, hasSameSizeAs, hasSameSizeAs, hasSameSizeAs, hasSize, hasSizeBetween, hasSizeGreaterThan, hasSizeGreaterThanOrEqualTo, hasSizeLessThan, hasSizeLessThanOrEqualTo, hasToString, hasValueSatisfying, is, isEmpty, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEmpty, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNullOrEmpty, isOfAnyClassIn, isSameAs, noneSatisfy, overridingErrorMessage, size, usingComparator, usingComparator, usingDefaultComparator, usingDefaultElementComparator, usingElementComparator, usingRecursiveComparison, usingRecursiveComparison, withFailMessage, withThreadDumpOnError
-
Methods inherited from class org.assertj.core.api.AbstractObjectAssert
extracting, extracting, extracting, extracting, extracting, getComparatorsByType, hasAllNullFieldsOrProperties, hasAllNullFieldsOrPropertiesExcept, hasFieldOrProperty, hasFieldOrPropertyWithValue, hasNoNullFieldsOrProperties, hasNoNullFieldsOrPropertiesExcept, isEqualToComparingFieldByField, isEqualToComparingFieldByFieldRecursively, isEqualToComparingOnlyGivenFields, isEqualToIgnoringGivenFields, isEqualToIgnoringNullFields, newObjectAssert, returns, usingComparatorForFields, usingComparatorForType, withAssertionState, withComparatorByPropertyOrField, withTypeComparator
-
Methods inherited from class org.assertj.core.api.AbstractAssert
asInstanceOf, asList, assertionError, asString, descriptionText, equals, failWithMessage, getWritableAssertionInfo, hashCode, hasSameHashCodeAs, inBinary, inHexadecimal, isInstanceOfSatisfying, isNull, matches, matches, newListAssertInstance, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, throwAssertionError, withRepresentation
-
-
-
-
Method Detail
-
contains
@SafeVarargs public final MapAssert<KEY,VALUE> contains(Map.Entry<? extends KEY,? extends VALUE>... entries)
Description copied from class:AbstractMapAssert
Verifies that the actual map contains the given entries, in any order.This assertion succeeds if both actual map and given entries are empty.
Example :
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));
-
containsAnyOf
@SafeVarargs public final MapAssert<KEY,VALUE> containsAnyOf(Map.Entry<? extends KEY,? extends VALUE>... entries)
Description copied from class:AbstractMapAssert
Verifies that the actual map contains at least one of the given entries.Example :
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));
- Overrides:
containsAnyOf
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- Parameters:
entries
- the given entries.- Returns:
this
assertion object.
-
containsOnly
@SafeVarargs public final MapAssert<KEY,VALUE> containsOnly(Map.Entry<? extends KEY,? extends VALUE>... entries)
Description copied from class:AbstractMapAssert
Verifies that the actual map contains only the given entries and nothing else, 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); // assertion will pass assertThat(ringBearers).containsOnly(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf), entry(vilya, elrond)); // assertion will fail assertThat(ringBearers).containsOnly(entry(oneRing, frodo), entry(nenya, galadriel));
- Overrides:
containsOnly
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- Parameters:
entries
- the entries that should be in the actual map.- Returns:
this
assertions object
-
containsExactly
@SafeVarargs public final MapAssert<KEY,VALUE> containsExactly(Map.Entry<? extends KEY,? extends VALUE>... entries)
Description copied from class:AbstractMapAssert
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
, preferAbstractMapAssert.containsOnly(java.util.Map.Entry...)
in that case).Example :
Map<Ring, TolkienCharacter> ringBearers = newLinkedHashMap(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf)); // assertion will pass assertThat(ringBearers).containsExactly(entry(oneRing, frodo), entry(nenya, galadriel), entry(narya, gandalf)); // assertion will fail as actual and expected order differ assertThat(ringBearers).containsExactly(entry(nenya, galadriel), entry(narya, gandalf), entry(oneRing, frodo));
- Overrides:
containsExactly
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- Parameters:
entries
- the given entries.- Returns:
this
assertions object
-
containsKeys
@SafeVarargs public final MapAssert<KEY,VALUE> containsKeys(KEY... keys)
Description copied from class:AbstractMapAssert
Verifies that the actual map contains the given keys.Example :
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);
- Overrides:
containsKeys
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- Parameters:
keys
- the given keys- Returns:
this
assertions object
-
containsOnlyKeys
@SafeVarargs public final MapAssert<KEY,VALUE> containsOnlyKeys(KEY... keys)
Description copied from class:AbstractMapAssert
Verifies that the actual map contains only the given keys and nothing else, 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); // assertion will pass assertThat(ringBearers).containsOnlyKeys(oneRing, nenya, narya, vilya); // assertion will fail assertThat(ringBearers).containsOnlyKeys(oneRing, nenya);
- Overrides:
containsOnlyKeys
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- Parameters:
keys
- the given keys that should be in the actual map.- Returns:
this
assertions object
-
containsValues
@SafeVarargs public final MapAssert<KEY,VALUE> containsValues(VALUE... values)
Description copied from class:AbstractMapAssert
Verifies that the actual map contains the given values.Example :
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);
- Overrides:
containsValues
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- Parameters:
values
- the values to look for in the actual map.- Returns:
this
assertions object
-
doesNotContainKeys
@SafeVarargs public final MapAssert<KEY,VALUE> doesNotContainKeys(KEY... keys)
Description copied from class:AbstractMapAssert
Verifies that the actual map does not contain any of the given keys.Example :
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);
- Overrides:
doesNotContainKeys
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- Parameters:
keys
- the given keys- Returns:
this
assertions object
-
doesNotContain
@SafeVarargs public final MapAssert<KEY,VALUE> doesNotContain(Map.Entry<? extends KEY,? extends VALUE>... entries)
Description copied from class:AbstractMapAssert
Verifies that the actual map does not contain the given entries.Example :
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));
- Overrides:
doesNotContain
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- Parameters:
entries
- the given entries.- Returns:
this
assertion object.
-
extracting
@SafeVarargs public final AbstractListAssert<?,List<?>,Object,ObjectAssert<Object>> extracting(Function<? super Map<KEY,VALUE>,?>... extractors)
Description copied from class:AbstractObjectAssert
Uses the givenFunction
s to extract the values from the object under test into a list, this new list becoming the object under test.If the given
Function
s extract the id, name and email values then the list will contain the id, name and email values of the object under test, you can then perform list assertions on the extracted values.Example:
// Create frodo, setting its name, age and Race (Race having a name property) TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT); // let's verify Frodo's name, age and race name: assertThat(frodo).extracting(TolkienCharacter::getName, character -> character.age, // public field character -> character.getRace().getName()) .containsExactly("Frodo", 33, "Hobbit");
Note that the order of extracted values is consistent with the order of given extractor functions.
- Overrides:
extracting
in classAbstractObjectAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>>
- Parameters:
extractors
- the extractor functions to extract values from the Object under test.- Returns:
- a new assertion object whose object under test is the list containing the extracted values
-
extractingByKeys
@SafeVarargs public final AbstractListAssert<?,List<? extends VALUE>,VALUE,ObjectAssert<VALUE>> extractingByKeys(KEY... keys)
Description copied from class:AbstractMapAssert
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.
- Overrides:
extractingByKeys
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- 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
-
extractingFromEntries
@SafeVarargs public final AbstractListAssert<?,List<? extends Tuple>,Tuple,ObjectAssert<Tuple>> extractingFromEntries(Function<? super Map.Entry<KEY,VALUE>,Object>... extractors)
Description copied from class:AbstractMapAssert
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
AbstractMapAssert.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 familly 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.- Overrides:
extractingFromEntries
in classAbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
- 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.
-
-