I have a unit test that needs to check for a nested map value. I can get my assertion to work by pulling out the entry and matching the underlying Map
, but I was looking for a clear way to show what the assertion is doing. Here is a very simplified test:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class MapContainsMapTest {
@Test
public void testMapHasMap() {
Map<String, Object> outerMap = new HashMap<String, Object>();
Map<String, Object> nestedMap = new HashMap<String, Object>();
nestedMap.put("foo", "bar");
outerMap.put("nested", nestedMap);
// works but murky
assertThat((Map<String, Object>) outerMap.get("nested"), hasEntry("foo", "bar"));
// fails but clear
assertThat(outerMap, hasEntry("nested", hasEntry("foo", "bar")));
}
}
It seems the problem is the outer map is being compared using hasEntry(K key, V value)
while what I want to use is hasEntry(Matcher<? super K> keyMatcher, Matcher<? super V> valueMatcher)
. I am not sure how to coerce the assertion to use the second form.
Thanks in advance.
hasEntry(is("key"), is("value"))
for example would cause the second form to be used. Maybe you could useequalTo
matcher in place ofis
, but either way, the readability goes out of the window. – AmbagiousequalTo("nested")...
but that doesn't work unless I change the outer map type toMap<String, Map<String,Object>>
– Roundlet