public class MapAssert<KEY,VALUE> extends AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
Map
s.
To create a new instance of this class, invoke
.
Assertions.assertThat(Map)
actual, info, myself, throwUnsupportedExceptionOnEquals
Modifier and Type | Method and 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>,Object>... extractors)
Uses the given
Function s to extract the values from the object under test into a list, this new list becoming
the object under test. |
AbstractListAssert<?,List<? extends Tuple>,Tuple,ObjectAssert<Tuple>> |
extractingFromEntries(Function<? super Map.Entry<KEY,VALUE>,Object>... extractors)
|
allSatisfy, anySatisfy, as, as, containsAllEntriesOf, containsEntry, containsExactlyEntriesOf, containsKey, containsOnlyKeys, containsValue, describedAs, describedAs, doesNotContainEntry, doesNotContainKey, doesNotContainValue, doesNotHave, doesNotHaveSameClassAs, extracting, 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, withFailMessage, withThreadDumpOnError
extracting, extracting, getComparatorsByType, hasAllNullFieldsOrProperties, hasAllNullFieldsOrPropertiesExcept, hasFieldOrProperty, hasFieldOrPropertyWithValue, hasNoNullFieldsOrProperties, hasNoNullFieldsOrPropertiesExcept, isEqualToComparingFieldByField, isEqualToComparingFieldByFieldRecursively, isEqualToComparingOnlyGivenFields, isEqualToIgnoringGivenFields, isEqualToIgnoringNullFields, newObjectAssert, returns, usingComparatorForFields, usingComparatorForType, usingRecursiveComparison, usingRecursiveComparison
asList, asString, descriptionText, equals, failWithMessage, getWritableAssertionInfo, hashCode, hasSameHashCodeAs, inBinary, inHexadecimal, isInstanceOfSatisfying, isNull, matches, matches, newListAssertInstance, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, throwAssertionError, withRepresentation
@SafeVarargs public final MapAssert<KEY,VALUE> contains(Map.Entry<? extends KEY,? extends VALUE>... entries)
AbstractMapAssert
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));
@SafeVarargs public final MapAssert<KEY,VALUE> containsAnyOf(Map.Entry<? extends KEY,? extends VALUE>... entries)
AbstractMapAssert
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));
containsAnyOf
in class AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
entries
- the given entries.this
assertion object.@SafeVarargs public final MapAssert<KEY,VALUE> containsOnly(Map.Entry<? extends KEY,? extends VALUE>... entries)
AbstractMapAssert
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));
containsOnly
in class AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
entries
- the entries that should be in the actual map.this
assertions object@SafeVarargs public final MapAssert<KEY,VALUE> containsExactly(Map.Entry<? extends KEY,? extends VALUE>... entries)
AbstractMapAssert
HashMap
, prefer AbstractMapAssert.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));
containsExactly
in class AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
entries
- the given entries.this
assertions object@SafeVarargs public final MapAssert<KEY,VALUE> containsKeys(KEY... keys)
AbstractMapAssert
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);
containsKeys
in class AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
keys
- the given keysthis
assertions object@SafeVarargs public final MapAssert<KEY,VALUE> containsOnlyKeys(KEY... keys)
AbstractMapAssert
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);
containsOnlyKeys
in class AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
keys
- the given keys that should be in the actual map.this
assertions object@SafeVarargs public final MapAssert<KEY,VALUE> containsValues(VALUE... values)
AbstractMapAssert
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);
containsValues
in class AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
values
- the values to look for in the actual map.this
assertions object@SafeVarargs public final MapAssert<KEY,VALUE> doesNotContainKeys(KEY... keys)
AbstractMapAssert
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);
doesNotContainKeys
in class AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
keys
- the given keysthis
assertions object@SafeVarargs public final MapAssert<KEY,VALUE> doesNotContain(Map.Entry<? extends KEY,? extends VALUE>... entries)
AbstractMapAssert
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));
doesNotContain
in class AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
entries
- the given entries.this
assertion object.@SafeVarargs public final AbstractListAssert<?,List<?>,Object,ObjectAssert<Object>> extracting(Function<? super Map<KEY,VALUE>,Object>... extractors)
AbstractObjectAssert
Function
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.
extracting
in class AbstractObjectAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>>
extractors
- the extractor functions to extract values from the Object under test.@SafeVarargs public final AbstractListAssert<?,List<? extends Tuple>,Tuple,ObjectAssert<Tuple>> extractingFromEntries(Function<? super Map.Entry<KEY,VALUE>,Object>... extractors)
AbstractMapAssert
Function
s to extract values from the Map
's entries.
The extracted values are stored in a new list composed of Tuple
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 the Map
entries.
That's why here the new object under test is a List of Tuple
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 :
// Build a Map that associates family roles and name of the Simpson familly
Map<String, CartoonCharacter> characters = new HashMap<>();
characters.put("dad", new CartoonCharacter("Omer"));
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", "Omer"),
tuple("mom", "Marge"),
tuple("girl", "Lisa"),
tuple("boy", "Bart"));
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 HashSet
, you won't be able to make any assumptions on the extracted values
order.extractingFromEntries
in class AbstractMapAssert<MapAssert<KEY,VALUE>,Map<KEY,VALUE>,KEY,VALUE>
extractors
- the extractor functions to extract values from an entry of the Map under test.Copyright © 2014–2019 AssertJ. All rights reserved.