2012-06-20 43 views
0

我無法找到一個答案已經在stackoverlow這個。這是我可以簡化的最簡單的例子。使用通用方法簽名Mockito(java,eclipse)v1.9

我有我需要模擬一個類,它會將調用這個類

public class GetCustomerForUser extends PropertyAction<Customer> ....

PropertyAction:

public abstract class PropertyAction<R> extends AbstractProcessAction<R> implements ValuedAction<R, R> {

真正的調用代碼看起來像這樣

Customer self = dispatcher.invokeTransactionless(actor, new GetCustomerForUser()); 調度程序類型ActionDispatcher

在調用模擬

protected void applyWhenClauses(ActionDispatcher dispatcher, Actor actor) throws ProcessException { 
     when(dispatcher.invokeTransactionless(actor, (ProcessAction<Customer>) anyObject())).thenReturn(null); 
     .... 

調度程序類型ActionDispatcherMock的

當我運行測試,我得到

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

    For more info see javadoc for Matchers class. 

    at com.prolog.test.mock.ActionDispatcherMock2.applyWhenClauses 
      (ActionDispatcherMock2.java:18) 
     at com.prolog.test.mock.ActionDispatcherMock.createMockInstance 
      (ActionDispatcherMock.java:84) 
at com.prolog.test.mock.ActionDispatcherMock.createMockInstance 
      (ActionDispatcherMock.java:1) 
at com.prolog.test.mockFactory.AbstractPrologInstanceMock.createInstanceMock 
      (AbstractPrologInstanceMock.java:11) 

我打算調度員可以調用每個類的時間。

有沒有人有任何關於我在這裏做錯的智慧的話?

感謝您的時間。

回答

1

該錯誤信息表明問題是什麼:如果你使用任何... any你不能再有一個不可─any匹配:

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

看你怎麼」重新做它說你不能做的事嗎?以及它如何改正它?

+0

好的 - 如果我理解正確,你認爲問題實際上是「演員」的參考? – Hollster

+0

@霍爾斯特那*是問題所在。 –

+0

是的。讓它成爲eq(演員)。匹配器是那些靜態方法,如eq()和anyObject(),它們定義了某個參數的期望值。當你傳遞一個對象時,像Mockito一樣對待eq(actor)。這就像速記。但是當你明確地通過一個匹配器,比如anyObject(),你的其他參數也必須是匹配器,以便Mockito可以確定你的意圖。 –