Calling real method in Mockito, but intercepting the result

Grzenio :

Simplifying a bit, our system has two parts. "Our" part, which in turn uses an lower level part implemented by another team (in the same codebase). We have a fairly complicated functional test setup, where we wrap the entry points to the lower level in spy objects. In positive tests we use the real implementation of that level, but we mock calls that should fail with some predefined error.

Now I am trying to add support for more complicated scenarios, where I would like to add an artificial delay for the calls made to the underlying level (on a fake clock obviously). To do this I would like to define a mock that would (1) Call the real implementation (2) Get the resulting Future object that is returned and combine it with a custom function that would inject the delay accordingly. So Ideally I would like to have something like:

doAnswer(invocationOnMock -> 
    { 
      result = call real method on mySpy; 
      return Futures.combile(result, myFunction);
    }).when(mySpy).myMethod();

How can I achieve it?

Sergii Bishyr :

As for me the easiest way it's just to save the link to the read object when you initialize your Spy object:

Foo realFoo = new Foo();
Foo spyFoo = Mockito.spy(realFoo);

Now you can stub it like this:

doAnswer(invocation -> realFoo.getSome() + "spyMethod").when(spyFoo).getSome();

One more way is to call invocation.callRealMethod():

doAnswer(invocation -> invocation.callRealMethod() + "spyMethod").when(spyFoo).getSome();

But in this case you may need to cast the return value as long as invocation.callRealMethod() returns Object.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=471079&siteId=1