title: RestTemplate mocking gives NullPointerException
body: <p>My service class code is as below:</p>
  
  <pre><code>public class MyServiceImpl implements MegatillAccessService {
  @Autowired
  RestTemplate restTemplate;
  
  @Value("${api.key}")
  private String apiKey;
  
  @Value("${customers.url}")
  private String postUrl;
  
  @Override
  public String pushCustomerData(List&lt;Customer&gt; listOfcustomers, String storeId) throws MyServiceException {
  
  Set&lt;Customer&gt; setOfCustomers = new HashSet&lt;&gt;(listOfcustomers);
      int noOfCustomersLoadedSuccessfully =0;
  
      MultiValueMap&lt;String, String&gt; headers = new LinkedMultiValueMap&lt;String, String&gt;();
      headers.add("apiKey", apiKey);
      headers.add("Content-Type", "application/json");
      headers.add("storeId", storeId);
      restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
  
      for(Customer customer: setOfCustomers){
          HttpEntity&lt;Customer&gt; request = new HttpEntity&lt;Customer&gt;(customer, headers);
          CustomerDataDto customerDataDto = null;
          try {
              customerDataDto = restTemplate.exchange(postUrl, HttpMethod.POST, request, CustomerDataDto.class).getBody();
          }
          catch (HttpClientErrorException ex) {
              if (ex.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
                  log.error("The customers service is not available to load data: "+ ex.getResponseBodyAsString(), ex);
                  throw new MyServiceException("The customers service is not available to load data",new RuntimeException(ex));
              }
              else{
                  log.warn("Error for customer with alias: "+customer.getAlias() +" with message: "+ ex.getResponseBodyAsString(), ex);
                  if(!ex.getResponseBodyAsString().contains("already found for this shop")){
                      throw new MyServiceException("An error occurred while calling the customers service with status code "+ex.getStatusCode(),new RuntimeException(ex));
                  }
              }
          }
          catch(Exception e){
              throw new MyServiceException("An error occurred while calling the customers service: ",new RuntimeException(e));
          }
  
          if(null != customerDataDto) {
              noOfCustomersLoadedSuccessfully++;
              log.debug("--------Data posted successfully for: ---------"+customerDataDto.getAlias());
          }
      }
      String messageToReturn = "No. of unique customers from source: "+setOfCustomers.size()+". No. of customers loaded to destination without error: "+noOfCustomersLoadedSuccessfully;
      return messageToReturn;
  }
  }
  </code></pre>
  
  <p>My Test class is as below:</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  @SpringBootTest
  public class MyServiceTest {
  
  @InjectMocks
  private MyService myService = new MyServiceImpl();
  
  @Mock
  RestTemplate restTemplate;
  
  @Before
  public void setUp() throws Exception
  {
      MockitoAnnotations.initMocks(this);
      initliaizeModel();
      restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
  }
  
  @Test
  public void pushAllRecords(){
  
      Mockito.when(restTemplate.exchange(Matchers.anyString(), Matchers.any(HttpMethod.class), Matchers.&lt;HttpEntity&lt;?&gt;&gt; any(), Matchers.&lt;Class&lt;CustomerDataDto&gt;&gt; any()).getBody()).thenReturn(customerDataDto);
  
      /*Mockito.when(restTemplate.exchange(Mockito.anyString(),
              Mockito.&lt;HttpMethod&gt; eq(HttpMethod.POST),
              Matchers.&lt;HttpEntity&lt;?&gt;&gt; any(),
              Mockito.&lt;Class&lt;CustomerDataDto&gt;&gt; any()).getBody()).thenReturn(customerDataDto);*/
  
      String resultReturned = myService.pushCustomerData(customers,"1235");
      assertEquals(resultReturned, "No. of unique customers from source: 2. No. of customers loaded to destination without error: 2");
  }
  
  }
  </code></pre>
  
  <p>While running the test, I am getting NullPointerException at the line where I am giving the Mockito.when and thenReturn condition. I tried many combinations but it is still giving NPE. I can't even reach the method invocation.Can you please let me know where am I going wrong?</p>