title: Mock a DAO class and a method within it
body: <p>I am trying to mock a DAO class that has a method which returns a list of a specific class. </p>
  
  <pre><code>private List&lt;SpecificClass&gt; getInfo(){
  
  List&lt;SpecificClass&gt; returnInformation = dao.list(ParamOne, Param Two, SpecificClass.class);
  }
  </code></pre>
  
  <p>The dao mentioned in the above method refers to another class.</p>
  
  <p>I begin by mocking that DAO class.</p>
  
  <p><code>Mockito.mock(TheDaoClass.class);</code></p>
  
  <p>and creating a mocked list</p>
  
  <p><code>private @Mock List&lt;SpecificClass&gt; returnedList = new ArrayList&lt;SpecificClass&gt;();</code></p>
  
  <p>Then I make call to that method </p>
  
  <p><code>dao.list(ParamOne, Param Two, SpecificClass.class);</code></p>
  
  <p>and specify what needs to be done when it is called </p>
  
  <p><code>when(dao.list(ParameterOne, anyString(), SpecificClass.class)).thenReturn(returnedList);</code></p>
  
  <p>When I do the above I get a null pointer exception. There can be two causes: </p>
  
  <p>I understand the list is empty but all it is supposed is hold SpecificClass's five values but that shouldn't throw an error at this point.</p>
  
  <p>I think object <code>dao</code> is not getting mocked properly. I am only mocking the whole class <code>Mockito.mock(TheDaoClass.class)</code> in order to mock any object asociated with that class. I think that it is not achieving the objective. how do I go about solving this problem? Any help is appreciated. Thanks. </p>