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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -