0

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?

Chen
  • 860
  • 10
  • 32
  • Does this answer your question? [Equivalent of Answers.RETURNS\_DEEP\_STUBS for a spy in mockito](https://stackoverflow.com/questions/47822542/equivalent-of-answers-returns-deep-stubs-for-a-spy-in-mockito) – Jonasz Sep 27 '22 at 05:34
  • Well, the example I gave is essentially what the post you gave is doing. Wondering if there is a better way of doing it. – Chen Sep 27 '22 at 16:43

0 Answers0