title: Mockito : java.lang.NullPointerException when calling method from mocked Class
body: <p>I am using mockito to write some unit tests for an application which is already tested with integration testing but we need also to develop unit tests.</p>
  
  <p>This is the code for the testing :</p>
  
  <pre><code>public class TestResourceB {
  
  @Mock
  ResourceB b;
  @Mock
  ResourceC c;
  
  @Before
  public void setup() {
      MockitoAnnotations.initMocks(this);
      TestObjects.InitializeObjects();
  }
  
  @Test
  public void testMethodResourceA() {
      when(b.callFuncA()).thenCallRealMethod();
      when(b.callFuncB()).thenReturn(TestObjects.mockedListA);
      when(b.callFuncC((B)anyVararg())).thenCallRealMethod();
      when(c.callFuncB()).thenReturn(TestObjects.mockedListB);
      when(c.callFuncA()).thenCallRealMethod
      String output = b.callFuncA();
  }
  }
  </code></pre>
  
  <p>This is the class ResourceB</p>
  
  <pre><code>public class ResourceB {
  
  ResourceC c = new ResourceC();
  
  public String callFuncA(){
      /*Calling callFuncB and doing some other stuff*/
      String test = callFuncC(arg1);
  }
  
  public List&lt;A&gt; callFuncB() {
      /*returns the mocked list A*/
  }
  
  public String callFuncC(B arg1) {
  String test2 = c.callFuncA();   // It crashes here
  /*doing some other stuff*/
  }
  }
  </code></pre>
  
  <p>This is the class ResourceC</p>
  
  <pre><code>public class ResourceC {
  public String callFuncA() {
      /*Calling callFuncB and doing some other stuff*/
      return testString;
  }
  
  public List&lt;B&gt; callFuncB() {
  /*return a List*/
  }
  }
  </code></pre>
  
  <p>The problem that I have is that in method callFuncC in class ResourceB when the line </p>
  
  <pre><code>String test2 = c.callFuncA();
  </code></pre>
  
  <p>is called I get a NullPointerException</p>
  
  <p>Any idea why this could be happening ?</p>