title: How to use Mockito when we cannot pass a mock object to an instance of a class
body: <p>Suppose I have a class like that:</p>
  
  <pre><code>public class MyClass {
  
      Dao dao;
  
      public String myMethod(Dao d) {
  
          dao = d;
  
          String result = dao.query();
  
          return result;
      } 
  }
  </code></pre>
  
  <p>I want to test it with mockito. So I create a mock object and I call the method to test in that way:</p>
  
  <pre><code>Dao mock = Mockito.mock(Dao.class);
  
  Mockito.when(mock.myMethod()).thenReturn("ok");
  
  new MyClass().myMethod(mock);
  </code></pre>
  
  <p>But, suppose instead I have a class like that:  </p>
  
  <pre><code>public class MyClass {
  
      Dao dao = new Dao();
  
      public String myMethod() {
  
          String result = dao.query();
  
          return result;
      } 
  }
  </code></pre>
  
  <p>Now I cannot pass my mock as an argument, so how I gonna test my method? Can someone show an example?</p>