2016-12-13 74 views
1

當我運行下面的JUnit測試:「沒有合適的方法,找到」使用hasItem Hamcrest匹配器時

@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) 

它看起來像在仿製藥的東西被打破。它是什麼?

回答

0

javac不知道如何推斷某些方法的泛型類型。

assertThat預計值和該類型的Matcher。你必須要明確

Assert.assertThat(data, Matchers.<Map<String, String>> hasItem(Matchers.hasKey("x"))); 

雖然這應該沒有明確的類型參數在Java中8

工作
相關問題