为什么不允许assertThat(map1,sameInstance(map2))?

时间:2015-04-14 07:04:26

标签: java unit-testing junit hamcrest

使用新的Assert语法,在测试身份时,可以编写

Assert.assertThat(obj1, CoreMatchers.sameInstance(obj2))

而不是

Assert.assertSame(obj1, obj2)

我正在尝试断言地图的身份。所以我写了

Assert.assertThat(map1, CoreMatchers.sameInstance(map2))

其中地图的类型为HashMap<String,String> 但我的测试在编译时失败了:

Error:(33, 9) error: no suitable method found for assertThat(Map,Matcher<Map<String,String>>)
method Assert.<T#1>assertThat(String,T#1,Matcher<? super T#1>) is not applicable
(cannot infer type-variable(s) T#1
(actual and formal argument lists differ in length))
method Assert.<T#2>assertThat(T#2,Matcher<? super T#2>) is not applicable
(cannot infer type-variable(s) T#2
(argument mismatch; Matcher<Map<String,String>> cannot be converted to Matcher<? super Map>))
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>assertThat(String,T#1,Matcher<? super T#1>)
T#2 extends Object declared in method <T#2>assertThat(T#2,Matcher<? super T#2>)
Error:(33) error: no suitable method found for assertThat(Map,Matcher<Map<String,String>>)

为什么JUnit(或Hamcrest)无法确定使用哪个匹配器?

1 个答案:

答案 0 :(得分:1)

事实证明这与声明地图的身份无关 - 代码是正确的 - 但它确实与泛型有关。

我试图测试的课程看起来像这样:

public class Response<T>{
    public final Map<String, String> map;
    public final T data;
}

然而,测试是这样写的:

@Test
public void testStuff() throws Exception {
    Map<String, String> map = new HashMap<>();
    Object data = new Object();
    Response target = new Response<>(map, data);
    assertThat(target.map, sameInstance(map));
    assertThat(target.data, sameInstance(data));
}

编译错误实际上在最后一行,因为编译器的T是未知的(<?>),它找不到合适的匹配器。我通过声明原始类型来修复测试。

@Test
public void testStuff() throws Exception {
    Map<String, String> map = new HashMap<>();
    Object data = new Object();
    Response<Object> target = new Response<>(map, data);
    assertThat(target.map, sameInstance(map));
    assertThat(target.data, sameInstance(data));
}

但是我觉得编译器抱怨前一行的原因很奇怪......