java - Calling real method in Mockito, but intercepting the result -




simplifying bit, our system has 2 parts. "our" part, in turn uses lower level part implemented team (in same codebase). have complicated functional test setup, wrap entry points lower level in spy objects. in positive tests use real implementation of level, mock calls should fail predefined error.

now trying add support more complicated scenarios, add artificial delay calls made underlying level (on fake clock obviously). define mock (1) call real implementation (2) resulting future object returned , combine custom function inject delay accordingly. ideally have like:

doanswer(invocationonmock ->      {        result = call real method on myspy;        return futures.combile(result, myfunction);     }).when(myspy).mymethod(); 

how can achieve it?

as me easiest way it's save link read object when initialize spy object:

foo realfoo = new foo(); foo spyfoo = mockito.spy(realfoo); 

now can stub this:

doanswer(invocation -> realfoo.getsome() + "spymethod").when(spyfoo).getsome(); 

one more way call invocation.callrealmethod():

doanswer(invocation -> invocation.callrealmethod() + "spymethod").when(spyfoo).getsome(); 

but in case may need cast return value long invocation.callrealmethod() returns object.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -