title: Handle anonymous class with Generics in Mockito
body: <p>I am trying to write unit test using Powermockito for following method -</p>
  
  <pre><code>public String getGenerator(String json) throws IOException {
      String jwt = "";
      ObjectMapper mapper = new ObjectMapper();
  
      // convert JSON string to Map
      Map&lt;String, Object&gt; map = mapper.readValue(json, new TypeReference&lt;Map&lt;String, Object&gt;&gt;() {
      }); // Here passing TypeReference annonymously
  
      // Create a JWT
      JWTGenerator generator = new JWTGenerator();
      if (map != null &amp;&amp; map.size() &gt; 0) {
          jwt = generator.createJWT(map);
      }
  
      return jwt;
  }
  </code></pre>
  
  <p>I have written test method as -</p>
  
  <pre><code>@Test
  public void testGetJWTGenerator() throws Exception {
      ObjectMapper mockMapper = PowerMockito.mock(ObjectMapper.class);
      PowerMockito.whenNew(ObjectMapper.class).withNoArguments().thenReturn(mockMapper);
  
      JWTGenerator mockJWTDecoder = PowerMockito.mock(JWTGenerator.class);
      PowerMockito.whenNew(JWTGenerator.class).withNoArguments().thenReturn(mockJWTDecoder);
  
      Map&lt;String, Object&gt; anyMap = new HashMap&lt;String, Object&gt;();
      anyMap.put("testStr", new Object());
  
      TypeReference&lt;Map&lt;String, Object&gt;&gt; mockTypeReference = (TypeReference&lt;Map&lt;String, Object&gt;&gt;) PowerMockito.mock(TypeReference.class);
      PowerMockito.whenNew(TypeReference.class).withNoArguments().thenReturn(mockTypeReference);
  
      PowerMockito.when(mockMapper.readValue(Mockito.anyString(), Mockito.eq(mockTypeReference))).thenReturn(anyMap);
      PowerMockito.when(mockJWTDecoder.createJWT(anyMap)).thenReturn(Mockito.anyString());
      utilityController = new UtilityController();
      utilityController.getJWTGenerator("{\"someStr\":\"someStr\"}");
      Mockito.verify(mockJWTDecoder, Mockito.times(1)).createJWT(anyMap);
  }
  </code></pre>
  
  <p>When I run this test I always get it failed saying -</p>
  
  <pre><code>Wanted but not invoked:
  jWTGenerator.createJWT(
      {testStr=java.lang.Object@24bdb479}
  );
  </code></pre>
  
  <p>It looks like stub is not working as I always get "null" for this line -</p>
  
  <pre><code>Map&lt;String, Object&gt; map = mapper.readValue(json, new TypeReference&lt;Map&lt;String, Object&gt;&gt;() {
          }); // Here passing TypeReference annonymously
  </code></pre>
  
  <p>Is it because anonymous instantiation of  TypeReference class?</p>