Java Mockito-InvalidUseofMatcherException

Java Mockito-InvalidUseofMatcherException,java,service,mocking,mockito,Java,Service,Mocking,Mockito,我尝试调试这段代码已经有一段时间了,但仍然没有成功。我继续抛出“InvalidUseofMatcherException”,专门针对以下代码: 对于设置: service = mock(Service.class); newRequest = mock(Request.class); when(service.newRequest(anyString(), anyString(), anyString())).thenReturn(

我尝试调试这段代码已经有一段时间了,但仍然没有成功。我继续抛出“InvalidUseofMatcherException”,专门针对以下代码:

对于设置:

        service = mock(Service.class);
        newRequest = mock(Request.class);
        when(service.newRequest(anyString(), anyString(), anyString())).thenReturn(
            newRequest);
在使用该服务的类中:

 Request newRequest = Service.newRequest(
            mId, "mp", itemID);
我假设它失败是因为我在when…thenReturn子句中传入了3“anyString()”,但可能是在硬编码的“mp”上失败了。因此,我尝试用以下内容替换when子句:

when(service.newRequest(anyString(), eq("mp"), anyString())).thenReturn(
            newRequest);
但仍然收到InvalidUseOfMatchersException

我是不是遗漏了mockito应该怎么做

完整堆栈:

    org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
3 matchers expected, 2 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 ServiceFacade.getSimilarities(ServiceFacade.java:29)
    at FacadeTest.getSimilarities(FacadeTest.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodImpl.invoke(DelegatingMethodImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
基于此语法:

Service.newRequest(mId, "mp", itemID);
看起来
newRequest
是一个静态方法。Mockito本质上是通过子类化(实际上是生成动态代理)工作的,因此它不适用于静态方法,因为静态方法不能通过子类化被重写。由于类似的原因,最终的方法是不可模仿的


如果正确,切换到工厂对象而不是工厂方法,这将允许您模拟工厂实例,或用于模拟静态字段。

请发布完整的异常堆栈跟踪和消息。添加@Sotiriosdelimanolist第一个和第三个代码段发生相同的异常?是的,两者都返回相同的异常@Sotiriosdelimanolis,具有相同的预期/记录计数?