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

Why is assertThat(map1, sameInstance(map2)) not allowed?

使用新的 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)无法确定要使用哪个匹配器?

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

我尝试测试的 class 看起来像这样:

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));
}

但我觉得奇怪为什么编译器会抱怨前一行...