title: Why does Mockito "when" work on a non-mock object?
body: <p>I recently saw some Mockito 1.9.5 code that worked like this:</p>
  
  <pre><code>MyObject myObject = new MyObject();
  ...
  Mockito.when(myObject.someMethod()).thenReturn("bogus");
  </code></pre>
  
  <p>Since myObject is <strong>not</strong> a mock object, but is an instance of a non-mocked class, I was surprised this compiled and ran without failing the unit test.  I expected I would get a failure saying something like "You asked me to set up an expectation on a non-mock object, and I expected to set expectations only on mock objects."</p>
  
  <p>Why doesn't this code cause a test failure?</p>
  
  <hr>
  
  <p><strong>Update:</strong> adding more code that is necessary to actually replicate the behavior I find confusing. These examples fully illustrate my question.  The following code behaves as I expected--when I run this test the test <strong>fails</strong> with a message that </p>
  
  <blockquote>
    <p>when() requires an argument which has to be 'a method call on a mock'.</p>
  </blockquote>
  
  <pre><code>public class AnotherObject{
      public String doSomething(){
          return "did something";
      };
  }
  
  public class MyObject{
      private AnotherObject anotherObject = new AnotherObject();
  
      public void setAnotherObject(AnotherObject anotherObject) {
          this.anotherObject = anotherObject;
      }
  
      public String someMethod(){
          return anotherObject.doSomething();
      }
  }
  
  @Test
  public void WhyDoesWhenWorkOnNonMock() throws Exception {
      MyObject myObject = new MyObject();
      Mockito.when(myObject.someMethod()).thenReturn("bogus");
  }
  </code></pre>
  
  <p>Now if I add a couple specific lines to this contrived test, <strong>the test no longer fails</strong> even though I expected the same failure and same message as before:</p>
  
  <pre><code>public class AnotherObject{
      public String doSomething(){
          return "did something";
      };
  }
  
  public class MyObject{
      private AnotherObject anotherObject = new AnotherObject();
  
      public void setAnotherObject(AnotherObject anotherObject) {
          this.anotherObject = anotherObject;
      }
  
      public String someMethod(){
          return anotherObject.doSomething();
      }
  }
  
  @Test
  public void WhyDoesWhenWorkOnNonMock() throws Exception {
      MyObject myObject = new MyObject();
      AnotherObject mockAnotherObject = Mockito.mock(AnotherObject.class);
      myObject.setAnotherObject(mockAnotherObject);
      Mockito.when(myObject.someMethod()).thenReturn("bogus");
  }
  </code></pre>