java - How can I generate a spy for an interface with Mockito without implementing a stub class? -
so have following interface:
public interface ifragmentorchestrator { void replacefragment(fragment newfragment, appaddress address); }
how can create spy
mockito allows me hook argumentcaptor
-objects calls replacefragment()
?
i tried
ifragmentorchestrator orchestrator = spy(mock(ifragmentorchestrator.class));
but mockito complains "mockito can mock visible & non-final classes."
the solution i've come far implement actual mock of interface before create spy
. kind of defeats purpose of mocking framework:
public static class emptyfragmentorchestrator implements ifragmentorchestrator { @override public void replacefragment(fragment newfragment, appaddress address) { } } public ifragmentorchestrator getspyobject() { return spy(new emptyfragmentorchestrator()); }
am missing fundamental? i've been looking through the docs without finding (but may blind).
spying when want overlay stubbing on existing implementation. here have bare interface, looks need mock
:
public class mytest { @captor private argumentcaptor<fragment> fragmentcaptor; @captor private argumentcaptor<appaddress> addresscaptor; @before public void setup() { mockitoannotations.initmocks(this); } @test public void testthing() { ifragmentorchestrator orchestrator = mock(ifragmentorchestrator.class); // call code should interact orchestrator // todo verify(orchestrator).replacefragment(fragmentcaptor.capture(), addresscaptor.capture()); // @ captors // todo } }
if, on other hand, trying spy on implementation, should that:
ifragmentorchestrator orchestrator = spy(new ifragmentorchestratorimpl());
this call actual methods on ifragmentorchestratorimpl
, still allow interception verify
.
Comments
Post a Comment