title: mock nested method calls using mockito
body: <p>I have got 4 classes lets says A, B, C, D each calling on methods from another one.</p>
  
  <p>now I have mocked class A, and want to mock a method using mockito</p>
  
  <pre><code>A a = Mockito.mock(A.class);
  </code></pre>
  
  <p>and want to get "foo" on recursive method calls like </p>
  
  <p><code>a.getB().getC().getD()</code> should return <code>"foo"</code></p>
  
  <p>I tried </p>
  
  <blockquote>
    <p>when(a.getB().getC().getD()).thenReturn("foo");</p>
  </blockquote>
  
  <p>but got nullPointerException</p>
  
  <p>then I tried</p>
  
  <blockquote>
    <p>doReturn("foo").when(a.getB().getC().getD());</p>
  </blockquote>
  
  <p>then I got  <code>org.mockito.exceptions.misusing.UnfinishedStubbingException:</code></p>
  
  <p>I know I can create objects of B, C and D, or can even write something like </p>
  
  <blockquote>
    <p>B b = mock(B.class) or A.setB(new B())</p>
  </blockquote>
  
  <p>and so on.</p>
  
  <p>But can't I do that in a single shot?
  Any help would be appreciated.</p>