title: Initialize private field in mocked class
body: <p>I have a private field in my class and a method which is modifying it if a certain flag is set AND if a map contains certain value. I want to test that method and to see the result. Here's the code (irrelevant fields and methods ommited for brevity):</p>
  
  <p><strong>My class:</strong></p>
  
  <pre><code>class UsersLoader {
     private Collection&lt;User&gt; users;
     private Map&lt;Integer,Boolean&gt; status;
  
     UsersLoader() {
        users = new ArrayList&lt;&gt;();
        status = new HashMap&lt;&gt;();
     }
  
     protected loadExternalUsers(boolean condition) {
        Collection&lt;User&gt; externalUsers = LoadUsersFromSomeExternalSource();
        if (condition &amp;&amp; status.get(SOME_KEY)) {
           users.addAll(externalUsers);
        } else {
           /* do not modify my users */
        }
     }
  }
  </code></pre>
  
  <p><strong>My test:</strong></p>
  
  <pre><code>import static org.mockito.Mockito.mock;
  
  public class UsersLoaderTest {
     private UsersLoader loader;
  
     @Before
     public void setUp() {
        loader = mock(UsersLoader.class);
        doCallRealMethod().when(loader).loadExternalUsers(anyBoolean());
     }
  
     @Test
     public void testLoadingWhenFlagIsTrue {
        Collection&lt;User&gt; users = loader.loadExternalUsers(true);
        assertThat(users.size() == 1);
     }
  
     @Test
     public void testLoadingWhenFlagIsFalse {
        Collection&lt;User&gt; users = loader.loadExternalUsers(false);
        assertThat(users.size() == 0);
     }
  
  }
  </code></pre>
  
  <p>When I run a test, I'm getting a <code>NullPointerException</code> in <code>UsersLoader</code> class, in the line with IF clause which checks the <code>status.get(SOME_KEY)</code>. Even if I solve that, the next NPE would be one line below, because my <code>users</code> would be null. How can I initialize private fields in my mocked class so I can work with them? I can create a getter and a setter and mock them as well, but then the whole test loses its point.</p>