mercredi 30 septembre 2015

How can I generate a spy for an interface with Mockito without implementing a stub class?

So I have the following interface:

public interface IFragmentOrchestrator {
    void replaceFragment(Fragment newFragment, AppAddress address);
}

How can I create a spy with mockito that allows me to hook ArgumentCaptor-objects to calls to replaceFragment()?

I tried

    IFragmentOrchestrator orchestrator = spy(mock(IFragmentOrchestrator.class));

But mockito complains with "Mockito can only mock visible & non-final classes."

The only solution I've come up with so far is to implement an actual mock of the interface before I create the spy. But that kind of defeats the purpose of a mocking framework:

public static class EmptyFragmentOrchestrator implements IFragmentOrchestrator {
    @Override
    public void replaceFragment(Fragment newFragment, AppAddress address) {

    }
}

public IFragmentOrchestrator getSpyObject() {
    return spy(new EmptyFragmentOrchestrator());
}

Am I missing something fundamental? I've been looking through the docs without finding anything (but I may be blind).

Aucun commentaire:

Enregistrer un commentaire