Class MapAssert<KEY,​VALUE>

    • Constructor Detail

      • MapAssert

        public MapAssert​(Map<KEY,​VALUE> actual)
    • 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));
        Overrides:
        contains in class AbstractMapAssert<MapAssert<KEY,​VALUE>,​Map<KEY,​VALUE>,​KEY,​VALUE>
        Parameters:
        entries - the given entries.
        Returns:
        this assertion object.
      • 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 class AbstractMapAssert<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 class AbstractMapAssert<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 with 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));
        Overrides:
        containsExactly in class AbstractMapAssert<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 class AbstractMapAssert<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 class AbstractMapAssert<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 class AbstractMapAssert<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 class AbstractMapAssert<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 class AbstractMapAssert<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 given Functions to extract the values from the object under test into a list, this new list becoming the object under test.

        If the given Functions 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 class AbstractObjectAssert<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 class AbstractMapAssert<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 given Functions to extract values from the Map's entries. The extracted values are stored in a new list composed of Tuples (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 Tuples.

        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("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"));
        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.
        Overrides:
        extractingFromEntries in class AbstractMapAssert<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.