title: Java Mockito with RestTemplate.exchange using Generics
body: <p>I have a generic method which invokes specified URL using <code>RestTemplate.exchange</code>. Method itself is working and loading data fine but I am not able to unit test it using Mockito.</p>
  
  <p><strong>Main Method</strong></p>
  
  <pre><code>@Service
  public class MyClass{
      private &lt;T&gt; List&lt;T&gt; loadData(String url) {
          return restTemplate.exchange(
              url, GET, null, new ParameterizedTypeReference&lt;List&lt;T&gt;&gt;(){}
          ).getBody().stream().collect(toList()));
      }
  }
  </code></pre>
  
  <p><strong>Unit Test</strong></p>
  
  <pre><code>@Runwith(MockitoJUnitRunner.class)
  public class MyTest {
      @Mock
      private RestTemplate restTemplate;
  
      @Test
      public void givenCall_myMethod_WillReturnData(){
          given(restTemplate.exchange(
              ArgumentMatchers.anyString(), ArgumentMatchers.any(), any(), any(Class.class)
          ))
          .willReturn(bodyData());
      }
  }
  </code></pre>
  
  <p>If I use non-generic version then everything works fine, however mockito returns <code>NullPointerException</code> with generics version.</p>
  
  <p>What's wrong or missing? </p>