Skip to content

Commit

Permalink
feat: add values methode in AbstractMapAssert to make assertions on v…
Browse files Browse the repository at this point in the history
…alues of the map (#3291)
  • Loading branch information
Kruschenstein committed Dec 14, 2023
1 parent fba37a3 commit 781844a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -2219,4 +2221,24 @@ private static List<Object> flatten(Iterable<Object> collectionToFlatten) {
}
return result;
}

/**
* <p>Returns an {@link AbstractCollectionAssert} to make assertions on the values of the map</p>
*
* <p><strong>Example</strong></p>
* <pre><code class='java'> TolkienCharacter pippin = new TolkienCharacter("Pippin", 28, HOBBIT);
* TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
* TolkienCharacter merry = new TolkienCharacter("Merry", 36, HOBBIT);
*
* Map&lt;String, TolkienCharacter&gt; characters = mapOf(entry("Pippin", pippin),
* entry("Frodo", frodo),
* entry("Merry", merry));
* assertThat(characters).values()
* .contains(frodo, pippin, merry); </code></pre>
* @return An {@link AbstractCollectionAssert} to make collections assertion only on map values.
*/
public AbstractCollectionAssert<?, Collection<? extends V>, V, ObjectAssert<V>> values() {
requireNonNull(actual, "Can not extract values from a null map.");
return assertThat(actual.values());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.assertj.core.api.map;

import org.assertj.core.api.AbstractCollectionAssert;
import org.assertj.core.api.ObjectAssert;
import org.junit.jupiter.api.Test;

import java.util.Collection;
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<String, Integer> map = Map.of("first", 1, "second", 2);
AbstractCollectionAssert<?, Collection<? extends Integer>, Integer, ObjectAssert<Integer>> assertion =
assertThat(map).values();
assertion.contains(1, 2);
}

@Test
void should_return_collection_assertion_with_right_generic_type() {
assertThat(Map.of("first", "one"))
.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<String, String> nullMap = null;
assertThatNullPointerException().isThrownBy(() -> assertThat(nullMap).values().contains("nothing"))
.withMessage("Can not extract values from a null map.");
}
}

0 comments on commit 781844a

Please sign in to comment.