I have the following line of code in Java:
boolean x = app1().app2().app3().returnBoolean();
Let's say app3
has several methods, but I would like to only mock the behavior of returnBoolean
inside app3 to always return true using Mockito. The other methods of app3
, app1
and app2
remain the same. Somehow I need to do a RETURN_DEEP_STUBS
by spying app3
. I did some research, and I think Mockito only supports RETURN_DEEP_STUBS
with mock.
I am able to simulate the behavior with the code below:
App1 app1Spy = spy(app1());
App2 app2Spy = spy(app1Spy.app2());
App3 app3Spy = spy(app2Spy.app3());
doReturn(app2Spy).when(app1Spy).app2();
doReturn(app3Spy).when(app2Spy).app3();
doReturn(true).when(app3Spy).returnBoolean();
But the code is rather lengthy. Is there another way of achieving this?