0

I have the test

 Document document = spy(new Document());
    Whitebox.setInternalState(documentReceiverInteractor, "document", document);

    String text= "string";

    Whitebox.invokeMethod(documentReceiverInteractor, "saveFields", anyString(), eq(text), anyString(),
            anyString(), anyString(), anyString(), anyString());

    verify(document).setText(text);

after startup, I get this error:

 Argument(s) are different! Wanted:
document.setText(
    <any string>
);
-> at ru.psbank.msb.dev.business.document.edit.receiver.DocumentReceiverInteractorTest.saveFields(DocumentReceiverInteractorTest.java:98)
Actual invocation has different arguments:
document.setText(
    null
);

eq works fine with primitives and there are no objects. What should I do?

pavel163
  • 145
  • 3
  • 7
  • have you tried to debug the test invocation? is null actually passed? – Maciej Kowalski Dec 15 '17 at 11:22
  • 1
    Why do you invoke methods with Matchers in the first place? What do you expect there to happen. `eq("text")` is not the String "text". Matchers are used in `verify`, `when`, etc. but not in actual method invocations. `anyString()` for example returns an empty String and not any String. `eq("text")` will probably return null when used in a direct invocation. You are simply using Matchers in the wrong place. Remove the matchers from your method invocations and everything will be fine. – Florian Schaetz Dec 15 '17 at 11:58

1 Answers1

2
Whitebox.invokeMethod(documentReceiverInteractor, "saveFields",
    anyString(), eq(text), anyString(),
    anyString(), anyString(), anyString(), anyString());

This statement doesn't make sense. Calls to anyString() and so forth are signals to Mockito that only make sense within calls to when and verify. Their return values are null, 0, "", or other dummy values, and their side effects are to modify Mockito's internal state; they are not random or arbitrary values for testing, and do not have any special behavior for Whitebox.

(Under the hood, you are calling setText with the return value from eq(text), which is null, and matching it against one of the calls to anyString() that you've accidentally added to the argument matcher stack.)

Instead, pick specific values:

Whitebox.invokeMethod(documentReceiverInteractor, "saveFields",
    "string 1",
    text,
    "string 2",
    "string 3",
    "string 4",
    "string 5",
    "string 6");

...and rather than using Whitebox, which is in Mockito's internal package org.mockito.internal.util.reflection and is deleted in Mockito 2.2, you should consider making the method call more visible (package-private if your test is in the same package, public if not). Your test is a consumer of your class, after all. If you choose to go that route, consider adding @VisibleForTesting or some other documentation (like /** Visible for testing. */).

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251