莫基托与任何(类型)方法不匹配



我测试了这段代码:

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) {
}

我相信这是因为您在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,您可能已经能够更好地调试它。

相关内容

  • 没有找到相关文章

最新更新