From d67586a0e1561a1e4113356c64d6cfad86a3536f Mon Sep 17 00:00:00 2001 From: Kruschenstein Date: Thu, 14 Dec 2023 21:39:03 +0100 Subject: [PATCH] feat: add values methode in AbstractMapAssert to make assertions on values of the map (#3291) --- .../assertj/core/api/AbstractMapAssert.java | 22 +++++++++ .../core/api/map/MapAssert_values_Test.java | 45 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 assertj-core/src/test/java/org/assertj/core/api/map/MapAssert_values_Test.java diff --git a/assertj-core/src/main/java/org/assertj/core/api/AbstractMapAssert.java b/assertj-core/src/main/java/org/assertj/core/api/AbstractMapAssert.java index abba3724579..aa24f159660 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/AbstractMapAssert.java +++ b/assertj-core/src/main/java/org/assertj/core/api/AbstractMapAssert.java @@ -15,6 +15,7 @@ import static java.util.Collections.singleton; import static java.util.Objects.requireNonNull; import static java.util.stream.Collectors.toList; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.description.Description.mostRelevantDescription; import static org.assertj.core.error.ShouldBeUnmodifiable.shouldBeUnmodifiable; @@ -27,6 +28,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; +import java.util.Collection; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; @@ -2219,4 +2221,24 @@ private static List flatten(Iterable collectionToFlatten) { } return result; } + + /** + *

Returns an {@link AbstractCollectionAssert} to make assertions on the values of the map

+ * + *

Example

+ *
 TolkienCharacter pippin = new TolkienCharacter("Pippin", 28, HOBBIT);
+   * TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
+   * TolkienCharacter merry = new TolkienCharacter("Merry", 36, HOBBIT);
+   *
+   * Map<String, TolkienCharacter> characters = mapOf(entry("Pippin", pippin),
+   *                                                  entry("Frodo", frodo),
+   *                                                  entry("Merry", merry));
+   * assertThat(characters).values()
+   *                       .contains(frodo, pippin, merry); 
+ * @return An {@link AbstractCollectionAssert} to make collections assertion only on map values. + */ + public AbstractCollectionAssert, V, ObjectAssert> values() { + requireNonNull(actual, "Can not extract values from a null map."); + return assertThat(actual.values()); + } } diff --git a/assertj-core/src/test/java/org/assertj/core/api/map/MapAssert_values_Test.java b/assertj-core/src/test/java/org/assertj/core/api/map/MapAssert_values_Test.java new file mode 100644 index 00000000000..d95ea797e10 --- /dev/null +++ b/assertj-core/src/test/java/org/assertj/core/api/map/MapAssert_values_Test.java @@ -0,0 +1,45 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * Copyright 2012-2023 the original author or authors. + */ +package org.assertj.core.api.map; + +import org.assertj.core.test.jdk11.Jdk11; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNullPointerException; +import static org.assertj.core.api.Assertions.from; + +class MapAssert_values_Test { + @Test + void should_return_collection_assertion_on_values_for_map_values() { + Map map = Jdk11.Map.of("first", 1, "second", 2); + assertThat(map).values().contains(1, 2); + } + + @Test + void should_return_collection_assertion_with_right_generic_type() { + Map map = Jdk11.Map.of("first", "one"); + assertThat(map).values() + .singleElement() + .returns('o', from(s -> s.charAt(0))); + } + + @Test + void should_have_an_helpful_error_message_when_size_is_used_on_a_null_map() { + Map nullMap = null; + assertThatNullPointerException().isThrownBy(() -> assertThat(nullMap).values().contains("nothing")) + .withMessage("Can not extract values from a null map."); + } +}