title: Testing a map contained in a mocked class
body: <p>I am trying to test a <code>ExampleController</code> class which works kind of like a facade for my POJO <code>Example</code> class.   </p>
  
  <p>I started to write a <code>InstrumentedTest</code> for the <code>ExampleController</code> because it works with Greenrobots EventBus, and got a <code>NullPointerException</code> when I tried to retrieve values which are stored in a <code>Map</code> in my <code>Example</code> class.<br>
  I use <code>Mockito v2.7.19</code>, <code>Espresso 2.2.2</code> and  <code>JUnit 4.12</code>.</p>
  
  <p>I recreated my problem in a Unit test with the following example setup:</p>
  
  <pre><code>class Example {
      private HashMap&lt;Integer, String&gt; map;
      Example(){ map = new HashMap&lt;&gt;(); }
      //getter and setter for map
  }
  
  class ExampleController {
      private Example example;
      ExampleController(Example example){ this.example = example; }
  
      public HashMap&lt;Integer, String&gt; getMap(){ return example.getMap(); }
  }
  </code></pre>
  
  <p>Test class: </p>
  
  <pre><code>class ExampleControllerTest {
      ExampleController exampleController;
  
      @Before
      public void setUp() throws Exception {
          Example example = mock(Example.class);
          exampleController = new ExampleController(example);
      }
  
      @Test
      public void testPutThingsInMap() throws Exception {
          HashMap&lt;Integer, String&gt; map = exampleController.getMap();
          map.put(1, "Test");
          exampleController.getMap().putAll(map);
  
          assertEquals(1, exampleController.getMap().size());
      }
  }
  </code></pre>
  
  <p>When I run the test class I get the following output:</p>
  
  <pre><code>java.lang.AssertionError: 
  Expected :1
  Actual   :0
  </code></pre>
  
  <p>As I am relatively new to testing, I don't know where I went wrong. When I search for unit testing lists, I only find tests for lists that are not contained in an object.</p>