Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add convenience methods for MapEntry assertions #117

Closed
wants to merge 1 commit into from

5 participants

@sberan

Quite often, I find myself testing map entries in the following way:

Map<String, String> address = ....
assertThat(address)
                .hasSize(15)
                .includes(
                        MapAssert.entry("zip", "43865"),
                        MapAssert.entry("lastName", "Frazier"),
                        MapAssert.entry("state", "IA"),
                        MapAssert.entry("city", "Culloden")
                );

This change allows me to simplify the map assertions by getting rid of the repeated MapAssert.entry calls:

Map<String, String> address = ....
assertThat(address)
                .hasSize(15)
                .includesEntry("zip", "43865")
                .includesEntry("lastName", "Frazier")
                .includesEntry("state", "IA")
                .includesEntry("city", "Culloden");

Please let me know if there's anything I can do to clean up the request!

Thanks for all your work on FEST! Assert and Reflect are life savers!
Sam

@sberan sberan Add convenience methods for MapEntry assertions:
 - containsEntry(K key, V value)
 - doesNotContainEntry(K key, V value)
02bd48e
@alexruiz
Owner

This is really, really nice. Thanks Sam!

Also, thank you for kind words about FEST :)

We are in the middle of a mega huge refactoring. Once we are done, I'll merge this pull requests.

Cheers,
-Alex

@alexruiz
Owner

I'll be removing the method includes(Entry) after merging your pull request.

@joel-costigliola
Collaborator

I like the idea, it makes it easier to check for entries than with MapAssert.entry parameters.
But on our latest version we use contains assertions instead of includes to be consistent with Iterables assertions.
So I think it should be changed to :

 Map<String, String> address = ....
 assertThat(address).hasSize(15)
                    .containsEntry("zip", "43865")
                    .containsEntry("lastName", "Frazier")
                    .containsEntry("state", "IA")
                    .containsEntry("city", "Culloden");

Moreover, we should also change doesNotContain(MapEntry... entries) to doesNotContainEntry(K, V)

@sberan
@alexruiz
Owner

Good point Joel! I agree with you.

Thanks Sam. I'll let you know if I find any issues while merging it.

@christophsturm

so what about the refactoring and this pull request? will it be merged soon?

@joel-costigliola
Collaborator

Alex is making a huge refactoring, contributions are in a pending state for the time being ...

@joel-costigliola joel-costigliola referenced this pull request in joel-costigliola/assertj-core
Closed

Improve MapEntry assertions #17

@jmnarloch

Hi,

First of all please take into consideration that includes() method error message is hard to read for multiple parameters that have different values. So instead we have asserting invidual keys:

Map<String, int> map = ...

assertThat(map.get("key1")).isEqualTo(1);
assertThat(map.get("key2")).isEqualTo(2);

Although such code looks very repetetive.

I have lately came up with similar idea as mentioned in this issue, although I have prototyped different API, which look like this:

Map<String, int> map = ...

assertThat(map)
               .entry("key1").isEqualTo(1)
               .entry("key2").isEqualTo(2)

Although there would be limitation to single predicate per entry, but we could simply return a subclass of AbstractAssert, so we could have a much grater flexibility.

If you like to I could prepare a pull request for that.

@sberan
@joel-costigliola
Collaborator

I like the latest ideas, I will probably add them in AssertJ core.
Just to let you know the convenience methods containsEntry and doesNotContainEntry has been added in AssertJ 1.2.0 release (AssertJ is a fork of Fest Assert).

Regards,

Joel Costigliola

@joel-costigliola joel-costigliola referenced this pull request in joel-costigliola/assertj-core
Open

More fluent Map assertions #70

@sberan sberan closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 12, 2012
  1. @sberan

    Add convenience methods for MapEntry assertions:

    sberan authored
     - containsEntry(K key, V value)
     - doesNotContainEntry(K key, V value)
This page is out of date. Refresh to see the latest.
View
24 src/main/java/org/fest/assertions/api/MapAssert.java
@@ -151,6 +151,30 @@ public void isEmpty() {
return this;
}
+ /**
+ * Verifies that the actual map contains an entry with the given key/value pair.
+ * @param key of the entry that should be in actual map.
+ * @param value of the entry that should be in actual map.
+ * @throws AssertionError if the actual map is {@code null}.
+ * @throws AssertionError if the actual map does not contain an entry with the given key/value pair.
+ */
+ public MapAssert<K, V> containsEntry(K key, V value) {
+ maps.assertContains(info, actual, new MapEntry[]{ MapEntry.entry(key, value)});
+ return this;
+ }
+
+ /**
+ * Verifies that the actual map does not contain an entry with the given key/value pair.
+ * @param key of the entry that should not be in actual map.
+ * @param value of the entry that should not be in actual map.
+ * @throws AssertionError if the actual map is {@code null}.
+ * @throws AssertionError if the actual map contains an entry with the given key/value pair.
+ */
+ public MapAssert<K, V> doesNotContainEntry(K key, V value) {
+ maps.assertDoesNotContain(info, actual, new MapEntry[]{ MapEntry.entry(key, value)});
+ return this;
+ }
+
/** {@inheritDoc} */
public MapAssert<K, V> usingElementComparator(Comparator<? super MapEntry> customComparator) {
throw new UnsupportedOperationException("custom element Comparator is not supported for MapEntry comparison");
View
29 src/test/java/org/fest/assertions/api/map/MapAssert_containsEntry_Test.java
@@ -0,0 +1,29 @@
+package org.fest.assertions.api.map;
+
+import org.fest.assertions.api.MapAssert;
+import org.fest.assertions.api.MapAssertBaseTest;
+import org.fest.assertions.data.MapEntry;
+
+import static org.fest.assertions.data.MapEntry.entry;
+import static org.fest.util.Arrays.array;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Tests for <code>{@link MapAssert#containsEntry(Object, Object)}}</code>.
+ *
+ * @author Sam Beran
+ */
+public class MapAssert_containsEntry_Test extends MapAssertBaseTest {
+
+ final MapEntry[] entries = array(entry("key1", "value1"));
+
+ @Override
+ protected MapAssert<Object, Object> invoke_api_method() {
+ return assertions.containsEntry("key1", "value1");
+ }
+
+ @Override
+ protected void verify_internal_effects() {
+ verify(maps).assertContains(getInfo(assertions), getActual(assertions), entries);
+ }
+}
View
30 src/test/java/org/fest/assertions/api/map/MapAssert_doesNotContainEntry_Test.java
@@ -0,0 +1,30 @@
+package org.fest.assertions.api.map;
+
+
+import org.fest.assertions.api.MapAssert;
+import org.fest.assertions.api.MapAssertBaseTest;
+import org.fest.assertions.data.MapEntry;
+
+import static org.fest.assertions.data.MapEntry.entry;
+import static org.fest.util.Arrays.array;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Tests for <code>{@link org.fest.assertions.api.MapAssert#doesNotContainEntry(Object, Object)}}</code>.
+ *
+ * @author Sam Beran
+ */
+public class MapAssert_doesNotContainEntry_Test extends MapAssertBaseTest {
+
+ final MapEntry[] entries = array(entry("key1", "value1"));
+
+ @Override
+ protected MapAssert<Object, Object> invoke_api_method() {
+ return assertions.doesNotContainEntry("key1", "value1");
+ }
+
+ @Override
+ protected void verify_internal_effects() {
+ verify(maps).assertDoesNotContain(getInfo(assertions), getActual(assertions), entries);
+ }
+}
Something went wrong with that request. Please try again.