1

I test this code:

        PublisherCallbackWithLog publisherCallback = new PublisherCallbackWithLog<String>();

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

in my test I have this line:

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

but I get an error, seems about the 2nd argument.

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)

How should I verify the call otherwise?

While debugging it never reaches my matchesSafely method:

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

    }
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • Is it just the lack of generics that is tripping you up? https://stackoverflow.com/q/7917635/3788176 – Andy Turner Jul 30 '17 at 11:15
  • What is your `eqWithoutRequestId(sdkRequest)` implementation and the values in `sdkRequest`? I have a feeling that it actually complains about the first argument not matching. – Adam Michalik Jul 30 '17 at 11:26
  • @AndyTurner I use Java 8 from your post: Using Java 8, you can simply use any() (assuming static import) without argument or type parameter because of enhanced type inference. The compiler now knows from the target type (the type of the method argument) that you actually mean Matchers.>any(), which is the pre-Java 8 solution. – Elad Benda Jul 30 '17 at 11:31
  • @AdamMichalik I add my code – Elad Benda Jul 30 '17 at 11:33

1 Answers1

0

I believe this is occurring because your generic type on RequestMatcher is WazeSdkRequest.SdkRequest, while the method you're calling (customPublisher.publish()) is expecting a String.

If you look at the code in TypeSafeMatcher.java, you can see that it first does a type check to see whether it should continue to the matchesSafely method:

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

In this case, I would expect expectedType.isInstance(item) to return false, which would cause your test to fail.

Also, you would likely have been able to debug this better if you actually implemented describeTo and overrode describerMismatchSafely.

Foxsly
  • 1,028
  • 6
  • 14