当我运行以下jUnit测试时:
@Test
public void test(){
List<Map<String,String>> data=new ArrayList<>();
Map<String,String> map=new HashMap<>();
map.put("x","y");
data.add(map);
assertThat(data, hasItem(hasKey("x")));
}
我明白了:
Error:(239, 9) java: no suitable method found for assertThat(java.util.List>,org.hamcrest.Matcher>>)
method org.junit.Assert.assertThat(java.lang.String,T,org.hamcrest.Matcher) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
method org.junit.Assert.assertThat(T,org.hamcrest.Matcher) is not applicable
(inferred type does not conform to upper bound(s)
inferred: java.util.List>
upper bound(s): java.lang.Iterable>,java.lang.Object)
看起来仿制药中的某些东西正在崩溃 . 它是什么?
1 回答
javac
不知道如何推断其中某些方法的泛型类型 .assertThat
需要一个值和该类型的Matcher
. 你必须明确虽然这应该在Java 8中没有显式类型参数的情况下工作 .