Skip to content

Instantly share code, notes, and snippets.

@htv2012
Created May 18, 2017 17:13
Show Gist options
  • Save htv2012/e9d4f40092739d8b22f104289ee34f13 to your computer and use it in GitHub Desktop.
Save htv2012/e9d4f40092739d8b22f104289ee34f13 to your computer and use it in GitHub Desktop.
# Custom Matcher to Compare Maps
package net.southeastwind.test;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static net.southeastwind.test.SameMapAs.sameMapAs;
public class CustomMatchersTest {
@Test
public void compareMaps() {
Map<String, String> expected = new HashMap<String, String>() {{
put("alias", "haiv");
put("uid", "501");
put("admin", "no");
}};
Map<String, String> actual = new HashMap<String, String>() {{
put("alias", "haiv");
put("uid", "502");
put("shell", "bash");
}};
assertThat("Error 6a3429f7", actual, is(sameMapAs(expected)));
// net.southeastwind.test.CustomMatchersTest > compareMaps FAILED
// java.lang.AssertionError: Error 6a3429f7
// Expected: is the same map as <{uid=501, alias=haiv, admin=no}>
// but:
// Values differ: actual: {"uid": "502"}, expected: {"uid": "501"}
// Missing from expected: {"shell": "bash"}
// Missing from actual: {"admin": "no"}
// Actual: <{uid=502, shell=bash, alias=haiv}>
// Expected: <{uid=501, alias=haiv, admin=no}>
}
@Test
public void compareMapsOfStringsToInts() {
Map<String, Integer> expected = new HashMap<String, Integer>() {{
put("abc", 1);
put("def", 2);
put("ghi", 3);
}};
Map<String, Integer> actual = new HashMap<String, Integer>() {{
put("abc", 1);
put("def", 20);
put("xyz", 10);
}};
assertThat("Error 24b120f5", actual, is(sameMapAs(expected)));
// net.southeastwind.test.CustomMatchersTest > compareMapsOfStringsToInts FAILED
// java.lang.AssertionError: Error 24b120f5
// Expected: is the same map as <{abc=1, def=2, ghi=3}>
// but:
// Values differ: actual: {"def": <20>}, expected: {"def": <2>}
// Missing from expected: {"xyz": <10>}
// Missing from actual: {"ghi": <3>}
// Actual: <{abc=1, def=20, xyz=10}>
// Expected: <{abc=1, def=2, ghi=3}>
}
}
package net.southeastwind.test;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
public class SameMapAs<T, U> extends TypeSafeDiagnosingMatcher<Map<T, U>> {
private Map<T, U> m_expected;
public SameMapAs(Map<T, U> expected) {
m_expected = expected;
}
public static <T, U> SameMapAs<T, U> sameMapAs(Map<T, U> expected) {
return new SameMapAs<T, U>(expected);
}
@Override
public void describeTo(Description description) {
description.appendText("the same map as ").appendValue(m_expected);
}
@Override
protected boolean matchesSafely(Map<T, U> actual, Description description) {
boolean matched = true;
Set<T> keys = new HashSet<>();
keys.addAll(actual.keySet());
keys.addAll(m_expected.keySet());
for (T key: keys) {
U expectedValue = m_expected.get(key);
U actualValue = actual.get(key);
if (null == expectedValue) {
matched = false;
description
.appendText("\n\t\tMissing from expected: {").appendValue(key)
.appendText(": ").appendValue(actualValue).appendText("}");
} else if (null == actualValue) {
matched = false;
description
.appendText("\n\t\tMissing from actual: {").appendValue(key)
.appendText(": ").appendValue(expectedValue).appendText("}");
} else if (!actualValue.equals(expectedValue)) {
matched = false;
description
.appendText("\n\t\tValues differ: ")
.appendText("actual: {").appendValue(key).appendText(": ").appendValue(actualValue).appendText("}")
.appendText(", expected: {").appendValue(key).appendText(": ").appendValue(expectedValue).appendText("}");
}
}
if (!matched) {
description
.appendText("\n\tActual: ")
.appendValue(actual)
.appendText("\n\tExpected: ")
.appendValue(m_expected)
.appendText("\n\t------------------------------");
}
return matched;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment