title: JUnit Mockito mock one method in another class Error
body: <p>I use Netbeans 8.1 , Junit and Mockito to write unit test for my project.
  Here is some pieces of my code</p>
  
  <p>To be tested function:</p>
  
  <pre><code>public Map&lt;String, String&gt; getAllUsers() {
  
      if (allUsers == null) {
          if (session.checkACL2("DonateBookPrivilegeLevel") || session.checkACL2("ManageUserPrivilegeLevel")) {
              Iterator&lt;User&gt; it = userFc.findAll().iterator();
              System.out.println("PC::enum()");
              allUsers  = new HashMap&lt;String, String&gt;();
              while (it.hasNext()) {
                  User item = it.next();
                  allUsers.put(item.getName(), item.getUserId().toString());
              }
          }
      }
      return allUsers;
  }
  </code></pre>
  
  <p>My Test class:</p>
  
  <pre><code>package com.controller;
  
  import com.entities.User;
  import com.jsfc.util.JsfUtil;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import javax.faces.event.ActionEvent;
  import org.junit.After;
  import org.junit.AfterClass;
  import org.junit.Before;
  import org.junit.BeforeClass;
  import org.junit.Test;
  import static org.junit.Assert.*;
  import org.junit.runner.RunWith;
  import static org.mockito.Matchers.any;
  import static org.mockito.Matchers.anyInt;
  import static org.mockito.Matchers.anyObject;
  import static org.mockito.Matchers.anyString;
  import org.mockito.Mock;
  import org.mockito.Mockito;
  import static org.mockito.Mockito.doReturn;
  import static org.mockito.Mockito.mock;
  import static org.mockito.Mockito.spy;
  import static org.mockito.Mockito.when;
  import org.mockito.MockitoAnnotations;
  import org.mockito.runners.MockitoJUnitRunner;
  
  
  @RunWith(MockitoJUnitRunner.class)
  public class PersonalCenterControllerTest {
  
      public PersonalCenterControllerTest() {
      }
  
      @BeforeClass
      public static void setUpClass() {
  
      }
  
      @AfterClass
      public static void tearDownClass() {
      }
  
      @Before
      public void setUp() {
           MockitoAnnotations.initMocks(this);
      }
  
      @After
      public void tearDown() {
      }  
  
      /**
       * Test of getAllUsers method, of class PersonalCenterController.
       */
      @Test
      public void testGetAllUsers() {
          System.out.println("getAllUsers");
  
          PersonalCenterController pcController = new PersonalCenterController();
  
          SessionController session = Mockito.mock(SessionController.class);
  
          when(session.checkACL2(anyString())).thenReturn(true).thenReturn(true);
  
          Map&lt;String,String&gt; userMap = null;
          userMap = pcController.getAllUsers();
  
      } 
  }
  </code></pre>
  
  <p>As you can see, I call session.checkACL2() which is defined in another class, I use <a href="https://stackoverflow.com/questions/31596913/using-mockito-to-mock-a-class-method-inside-another-class">Using Mockito to mock a class method inside another class</a> to fix </p>
  
  <pre><code>SessionController session = Mockito.mock(SessionController.class);
  doReturn(true).when(session).checkACL2((String) anyObject());
  </code></pre>
  
  <p>but it calls <code>NullPointerException</code> at if line.</p>