mockito不匹配任何(类型)方法

时间:2017-07-30 10:53:37

标签: java unit-testing testing mockito hamcrest

我测试了这段代码:

        PublisherCallbackWithLog publisherCallback = new PublisherCallbackWithLog<String>();

        for (SdkRequest.SdkRequest sdkRequest : SdkRequestsList.getRequestList()) {
            final String s = TextFormat.printToUnicodeString(sdkRequest);
            customPublisher.publish(s, publisherCallback);
        }

在我的测试中,我有这一行:

    verify(customPublisher, times(1)).publish(argThat(eqWithoutRequestId(sdkRequest)), any(PublisherCallbackWithLog.class));

但我得到一个错误,似乎是关于第二个论点。

Argument(s) are different! Wanted:
customPublisher.publish(
    ,
    <any>
);
-> at com.w.sdkService.servlets.SdkPollerServlet_PublishTest.readFromSpreadsheet3Rows_shouldPublish2Times(SdkPollerServlet_PublishTest.java:75)
Actual invocation has different arguments:
customPublisher.publish(
    "partner {
  display_name: "WTest"
  android_pkg_name: "com.example.eliran.myapplication"
  sharing_mode: DATA
}
requestId: "3a7458b6-edc0-4d4e-b52e-d2a3847bef0b"
requestType: REMOVE
",
    com.w.sdkService.services.callback.PublisherCallbackWithLog@56f6d40b
);
-> at com.w.sdkService.servlets.SdkPollerServlet.publishAddPartnersRequests(SdkPollerServlet.java:101)

Comparison Failure:  <Click to see difference>

Argument(s) are different! Wanted:
customPublisher.publish(
    ,
    <any>
);
-> at com.w.sdkService.servlets.SdkPollerServlet_PublishTest.readFromSpreadsheet3Rows_shouldPublish2Times(SdkPollerServlet_PublishTest.java:75)
Actual invocation has different arguments:
customPublisher.publish(
    "partner {
  display_name: "WTest"
  android_pkg_name: "com.example.eliran.myapplication"
  sharing_mode: DATA
}
requestId: "3a7458b6-edc0-4d4e-b52e-d2a3847bef0b"
requestType: REMOVE
",
    com.w.sdkService.services.callback.PublisherCallbackWithLog@56f6d40b
);
-> at com.w.sdkService.servlets.SdkPollerServlet.publishAddPartnersRequests(SdkPollerServlet.java:101)

我该如何验证电话?

调试时,它永远不会到达我的matchesSafely方法:

public class RequestMatcher extends TypeSafeMatcher<WazeSdkRequest.SdkRequest> {
    private WazeSdkRequest.SdkRequest expectedRequest;

    private RequestMatcher(final SdkRequest.SdkRequest request) {
        this.expectedRequest = request;
    }

    @Override
    protected boolean matchesSafely(final SdkRequest.SdkRequest sentRequest) {
        boolean answer = SdkRequest.newBuilder(sentRequest).clearRequestId().build()
                .equals(SdkRequest.newBuilder(expectedRequest).clearRequestId().build());

        return answer;
    }


    public static RequestMatcher eqWithoutRequestId(final SdkRequest request) {
        return new RequestMatcher(request);
    }

    @Override
    public void describeTo(final Description description) {

    }

1 个答案:

答案 0 :(得分:0)

我认为这是因为RequestMatcher上的通用类型是WazeSdkRequest.SdkRequest,而您调用的方法(customPublisher.publish())期待一个字符串。

如果查看TypeSafeMatcher.java中的代码,可以看到它首先进行类型检查,看它是否应该继续使用matchesSafely方法:

    public final boolean matches(Object item) {
        return item != null
            && expectedType.isInstance(item)
            && matchesSafely((T) item);
    }

在这种情况下,我希望expectedType.isInstance(item)返回false,这会导致测试失败。

此外,如果您实际实施describeTo并覆盖describerMismatchSafely,您可能会更好地进行调试。