mock nested method calls using mockito

I have got 4 classes lets says A, B, C, D each calling on methods from another one.

now I have mocked class A, and want to mock a method using mockito

A a = Mockito.mock(A.class);

and want to get "foo" on recursive method calls like

a.getB().getC().getD() should return "foo"

I tried

when(a.getB().getC().getD()).thenReturn("foo");

but got nullPointerException

then I tried

doReturn("foo").when(a.getB().getC().getD());

then I got org.mockito.exceptions.misusing.UnfinishedStubbingException:

I know I can create objects of B, C and D, or can even write something like

B b = mock(B.class) or A.setB(new B())

and so on.

But can't I do that in a single shot? Any help would be appreciated.


Solution 1:

Adding RETURNS_DEEP_STUBS did the trick:

A a = Mockito.mock(A.class, Mockito.RETURNS_DEEP_STUBS);

Solution 2:

The answer by Abhijeet is technically correct, but it is important to understand: you should not be doing this.

Your "production" code is heavily violating the Law of Demeter: your class A should not know that it has to get a B to get a C to get a D.

That simply leads to super tight coupling between all these classes. Not a good idea.

In that sense: you should see the fact that you need to do special things here to get your test working as an indication that your production code does something that is out of the normal.

So, instead of "fixing" your test setup, consider addressing the real problem. And that is the design of your production code!

And for the record: getB().getC().getD() is not a "recursive" call; it is more of a "fluent" chaining of method calls. And as said: that is not a good thing.