2012-12-15 75 views
2

運行AllOf Hamcrest匹配與Maven

assertThat(collection, allOf(hasItems(i1, i2, i3), hasSize(3)));

在Eclipse(運行爲 - > Junit的)一切正常,但是當我執行Maven的測試(mvn clean test),它在test-compile階段有以下的解釋

失敗
[ERROR] The method allOf(Matcher<? super T>, Matcher<? super T>) in the type AllOf<T> is not applicable for the arguments (Matcher<Iterable<Song>>, Matcher<Collection<? extends Object>>) 

的依賴是

<dependency> 
    <groupId>org.hamcrest</groupId> 
    <artifactId>hamcrest-all</artifactId> 
    <version>1.3</version> 
    <scope>test</scope> 
</dependency> 
<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit-dep</artifactId> 
    <version>4.10</version> 
    <scope>test</scope> 
    <exclusions> 
    <exclusion> 
     <artifactId>hamcrest-core</artifactId> 
     <groupId>org.hamcrest</groupId> 
    </exclusion> 
    </exclusions> 
</dependency> 

什麼時許ID錯了嗎?

感謝

斯特凡諾

回答

2

您必須爲方法hasItemshasSize指定類型的參數。編譯器自動類型推斷在您的情況下不起作用。要指定類型參數,您不能使用靜態導入的方法,但應使用它們的聲明類 - 例如Matchers.<Song>hasItems(i1,i2,i3)

+0

謝謝@SpaceTrucker –

相關問題