title: Nested mocking in Mockito
body: <p>I have this Mockito code:</p>
  
  <pre><code>interface Dao {
      public void doSomething();
  }
  
  class LegacyClass {
      Dao dao;
  
      public String legacyMethod() {
          dao.doSomething();
          return "Test";
      }
  }
  
  public class MockitoTest {
      public static void main(String[] args) {
          Dao dao = mock(Dao.class);
          LegacyClass legacyInst = new LegacyClass();
          legacyInst.dao = dao;
          LegacyClass legacy = spy(legacyInst);
  
          when(legacy.legacyMethod()).thenReturn("Replacement");
      }
  }
  </code></pre>
  
  <p>The last <code>when()</code> throws the following exception:</p>
  
  <pre><code>Exception in thread "main" org.mockito.exceptions.base.MockitoException: 
  'doSomething' is a *void method* and it *cannot* be stubbed with a *return value*!
  Voids are usually stubbed with Throwables:
      doThrow(exception).when(mock).someVoidMethod();
  If the method you are trying to stub is *overloaded* then make sure you are calling the right overloaded version.
      at mypkg.MockitoTest.main(MockitoTest.java:28)
  </code></pre>
  
  <p>However, I am NOT mocking return value for <code>Dao.doSomething</code>, but for <code>LegacyClass.legacyMethod()</code>.</p>
  
  <p>Is this the expected behavior? Are there any Mockito docs stating you cannot nest mocks like this?</p>
  
  <p>How can I walk this around?</p>