title: Junit/Mockito how to mock an object called from class b which is called from class a
body: <p>I have a class called a, which mocks a class b.</p>
  <p>Class b has a method which is creating an object by calling a data access object (DAO) c. That is,
  <code>a.method1 -&gt; b.method2</code> which internally calls <code>c.getdata()</code>.</p>
  <p>When I try to create a unit test I get object c is null. How do I resolve this issue?</p>
  <pre class="lang-java prettyprint-override"><code>  class Alpha {
        
      String abc;
      Beta beta = new Beta();
      List&lt;String&gt; seriesOfStgs = new ArrayList&lt;&gt;();
      
      public void alphaMethod() {
          seriesOfStgs.addAll(beta.getStrings());
      }
    }
  
    class Beta {
        
      StringDao stringDao = new StringDao();
      
      public List&lt;String&gt; getStrings() {
          return stringDao.getListOfStrings();
      }
    }
  </code></pre>
  <p>If you see above, I have 2 separate classes <code>Alpha</code> and <code>Beta</code>. I am writing the test for <code>Alpha</code>. I am able to mock <code>Beta</code> in my JUnit test, <code>TestAlpha.java</code>.
  I am getting a null pointer exception when I try to get the strings because <code>stringDao</code> is null.</p>
  <p>How do I instantiate <code>stringDao</code> in <code>TestAlpha</code>, or how do I pass a reference to <code>StringDao</code>?</p>