Deep wrap with doReturn method

I am trying to use the Mockito function for deep stamping using a method doReturn.

When I use a method when, as in the deep stubbing example, it works fine:

Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);
when(mock.getBar().getName()).thenReturn("deep");

But when I try to do the same with doReturn, I get WrongTypeOfReturnValue:

doReturn("deep").when(mock).getBar().getName();

I tried this too, but then I get UnfinishedStubbingException:

doReturn("deep").when(mock.getBar()).getName();
doReturn("deep").when(mock.getBar().getName());

How can I use the deep stamping function using the method doReturn?

(I know that the use of deep stubbing is not encouraged by some, including the Mockito developers. I'm not sure if I agree with their position on this issue. Let me leave this discussion out of this problem.)

+4
source share
1 answer

, Mockito , when. , mock.getBar() :

    Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);
    Bar bar = mock.getBar();
    doReturn("deep").when(bar).getName();
+4

All Articles