繁体   English   中英

使用 hamcrest 匹配 Map 包含不同类型的条目

[英]Using hamcrest to match Map contains entries of different types

假设我有一张地图:

Map<String,Object> map1 = new HashMap<String,Object>();
map1.put("foo1","foo1");
map1.put("foo2", Arrays.asList("foo2","bar2"));

现在我想使用 Hamcrest 匹配器来验证 Map 的值。 如果这是一个 Map< String,String > 我会做类似的事情:

assertThat(map1, hasEntry("foo1", "foo1"));

但是,在尝试将其与 Map 一起使用时,我遇到了困难,其中 Map 中的条目可能是字符串或值列表。 这适用于第一个条目:

assertThat(map1, hasEntry("foo1", (Object)"foo1"));

对于第二个条目,我不知道如何设置匹配器。

编辑:

我也试过这个,但它会产生编译器警告。

assertThat(
            map1,
            hasEntry(
                    "foo2",
                    contains(hasProperty("name", is("foo2")),
                            hasProperty("name", is("bar2")))));

“Assert 类型中的方法 assertThat(T, Matcher) 不适用于参数 (Map, Matcher>>>)”

(以上是这里的解决方案: Hamcrest compare collections

你不能用 Hamcrest hasEntry优雅地做到这一点,因为当你尝试在列表上使用匹配器时它会进行类型检查。

https://github.com/hamcrest/JavaHamcrest/issues/388上有一个功能请求

我认为最简单的选择是做这样的事情:

@Test
public void test() {
    Map<String, Object> map1 = new HashMap<>();
    map1.put("foo1", "foo1");
    map1.put("foo2", Arrays.asList("foo2", "bar2"));

    assertThat(map1, hasEntry("foo1", "foo1"));
    assertThat(map1, hasListEntry(is("foo2"), containsInAnyOrder("foo2", "bar2")));
}

@SuppressWarnings("unchecked")
public static org.hamcrest.Matcher<java.util.Map<String, Object>> hasListEntry(org.hamcrest.Matcher<String> keyMatcher, org.hamcrest.Matcher<java.lang.Iterable<?>> valueMatcher) {
    Matcher mapMatcher = org.hamcrest.collection.IsMapContaining.<String, List<?>>hasEntry(keyMatcher, valueMatcher);
    return mapMatcher;
}

hasListEntry在这里只是为了防止编译器错误。 它执行未经检查的分配,这就是您需要@SuppressWarnings("unchecked") 的原因。 例如,您可以将此静态方法放在常见的测试工具中。

尝试这种方式你可以使用 ImmutableMap

 assertThat( actualValue,
            Matchers.<Map<String, Object>>equalTo( ImmutableMap.of(
                "key1", "value",
                "key2", "arrayrelated values"
) ) );

希望它对你有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM