title: How to stub a method of an class annotated with @InjectMocks?
body: <p>Below <code>MyDictionary.get</code> method uses the injected <code>map</code> by calling <code>map.get</code>. </p>
  
  <p>Just of curiosity I stubbed the <code>MyDictionary.get</code> method, like I always do with mocks and spies, so I overwrite the injection.</p>
  
  <p>But this only works when   <code>MyDictionary.get</code> indeed calls <code>map.get</code>. If <code>map.get</code> returns some string (the empty string here), the stub <code>Mockito.when</code> does not work. The behavior is as if it is not there. In the assert line, <code>dictionary.get("key")</code> equals the empty string. This is what I do not understand.</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class MockitoTest {
  
      @Mock
      Map&lt;String, String&gt;map;
  
      @InjectMocks
      MyDictionary dictionary;
  
      @Test
      public void testMyDictionary(){
          Mockito.when(dictionary.get("key")).thenReturn("value");
          Assert.assertEquals("value", dictionary.get("key"));
      }
  
      private static class MyDictionary{
          private Map&lt;String, String&gt; map; 
          public String get(String key){
              return map.get(key);
  // or,
              return "";
          }
      }
  }
  </code></pre>