Can you explain Mockito's RETURNS_DEEP_STUBS and its use case?

RETURNS_DEEP_STUBS is a Mockito answer strategy that automatically generates mocks for every object returned along a method chain, so you can stub deeply nested calls without manually mocking each intermediate step.

Key Points

Example

For a.getB().getC().getName(), instead of mocking B and C individually, RETURNS_DEEP_STUBS lets you write when(a.getB().getC().getName()).thenReturn("test") directly.

A a = mock(A.class, RETURNS_DEEP_STUBS);
when(a.getB().getC().getName()).thenReturn("test");

assertEquals("test", a.getB().getC().getName());

Interview Tip

A concise interview answer is

RETURNS_DEEP_STUBS automatically creates intermediate mocks for every hop in a chained method call, so I can stub something like a.getB().getC().getName() in a single line instead of mocking each object manually. I use it sparingly, since long chains usually point to a design that could be simplified.

Last updated: 16 July 2026

Previous question: How do you mock objects in Mockito when using constructor injection?

Next question: How do you mock behavior for methods that depend on randomness (like Math.random())?

More Mockito interview questions

All Mockito questions