2015-04-27 26 views
9

我想這樣做:的Mockito Matchers.any(...)的一個參數只

verify(function, Mockito.times(1)).doSomething(argument1, Matchers.any(Argument2.class)); 

參數1類型的specfic實例ARGUMENT1參數2是任何實例類型參數2

但我得到一個錯誤:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 1 recorded. This exception may occur if matchers are combined with raw values: 
    //incorrect: 
    someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example: 
    //correct: 
    someMethod(anyObject(), eq("String by matcher")); 

按照他的意見,我可以寫下面的,一切都很好:

verify(function, Mockito.times(1)).doSomething(Matchers.any(Argument1.class), Matchers.any(Argument2.class)); 

我在哪裏尋找類型的任何arguement ARGUMENT1和任何類型的參數參數2

我該如何達到這個理想的行爲?

回答

13

有多個可能的參數匹配器,其中一個是eq,這是在異常消息中提到的。用途:

verify(function, times(1)).doSomething(eq(arg1), any(Argument2.class)); 

(應該是有靜態導入 - eq()Matchers.eq())。

你也有same()(它引用相等,即==),更一般地,你可以編寫自己的匹配器。

+0

盯着我的臉,讓我無需花時間閱讀!感謝那。 –