0

I am trying to test that an Interface method was called with and exact value passed in. I'm getting the following error:

org.mockito.exceptions.misusing.UnfinishedVerificationException: Missing method call for verify(mock) here: -> at com.example.app.initialize(example.java:136)

Example of correct verification: verify(mock).doSomething()

The error is being thrown on this verification line:

Mockito.verify(callback).onInitializeResult("initialized");

My Interface class:

public interface InitCallback {
/**
 * Returns whether or not the app was initialized.     .
 */
    void onInitializeResult(String result);
}

My Unit Test:

@Test
    public void initializationTest(){
        InitCallback callback = Mockito.spy(new InitCallback() {
            @Override
            public void onInitializeResult(String result) {

            }
        });
        mExample.initialize(mContext, callback);
        Mockito.verify(callback).onInitializeResult("initialized");
    }

I also tried to replace the callback mock with:

InitCallback callback = Mockito.mock(InitCallback.class);
Mockito.doNothing().when(callback).onInitializeResult(Mockito.anyString());

However I still get the same error. The interface only has 1 method and it is not final. Any Thoughts?

ChoklatStu
  • 229
  • 1
  • 10

1 Answers1

1

It turns out that mExample was calling another mocked instance whose methods were not defined. From another Stack Overflow post Link

Mockito throws exceptions if you misuse it so that you know if your tests are written correctly. The gotcha is that Mockito does the validation next time you use the framework (e.g. next time you verify, stub, call mock etc.). But even though the exception might be thrown in the next test, the exception message contains a navigable stack trace element with location of the defect. Hence you can click and find the place where Mockito was misused.

So in the case posted in the question, The exception was generated in

mExample.initialize(mContext, callback);

but not thrown until the next time verify was called.

ChoklatStu
  • 229
  • 1
  • 10