使用Mockito和通用方法签名(java,eclipse)v1.9

时间:2012-06-20 11:31:57

标签: java generics mocking mockito

我无法在stackoverlow上找到答案。这是我可以归结为最简单的例子。

我有一个我想要模拟的类,它会调用这个类

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 个答案:

答案 0 :(得分: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"));

看看你如何做到它说你不能做的事情?以及如何纠正它?