title: Mockito mock restTemplate not using returning mock value
body: <p>Relevant Code Below:</p>
  
  <p><strong>ServiceCode:</strong></p>
  
  <pre><code>@Override
  public ResponseEntity&lt;AppointmentResponse&gt; createAppointment(AppointmentRequest partnerFulfillmentRequest) {
  
      RestTemplate rt                                 = null;
      ResponseEntity&lt;AppointmentResponse&gt; response    = null;
      String uri                                      = null;
      HttpEntity&lt;AppointmentRequest&gt; httpEntity       = null;
      HttpHeaders headers = null;
      try {
              rt = new RestTemplate();
              rt.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
              rt.getMessageConverters().add(new StringHttpMessageConverter());
              uri = new String(internalServiceUrl+"/"+APP_NAME_INTERNAL+"/appointment");
              log.info("Calling internal service URL : "+uri);
              headers = new HttpHeaders();
              headers.setContentType(MediaType.APPLICATION_JSON);
              httpEntity = new HttpEntity&lt;AppointmentRequest&gt;(partnerFulfillmentRequest, headers);  
              response =  rt.exchange(uri, HttpMethod.PUT, httpEntity, AppointmentResponse.class);
              if (response != null)
              {
                  log.info("Got response from internal servicec--&gt;statusCode: "+response.getStatusCodeValue());
                  log.info("Got response from internal service--&gt; Body "+response.getBody());
              }
  
  
      }catch(HttpClientErrorException hceEx) {
          //hceEx.printStackTrace();
          AppointmentResponse res = new AppointmentResponse();
          return new ResponseEntity&lt;AppointmentResponse&gt;(mapResponse(hceEx.getResponseBodyAsString()), hceEx.getResponseHeaders(), hceEx.getStatusCode());
      }catch(Exception e) {
          e.printStackTrace();
          AppointmentResponse res = new AppointmentResponse();
          ResponseEntity&lt;AppointmentResponse&gt; wfmErrResponse = new ResponseEntity&lt;AppointmentResponse&gt;(res, HttpStatus.INTERNAL_SERVER_ERROR);
          log.error("ERROR WHILE CALLING INTERNAL SERVICE");
          log.error(uri);
          log.error(e);
          return wfmErrResponse;
      }
      return response;
  
  }
  </code></pre>
  
  <p><strong>Test Code:</strong></p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class PartnerFulfillmentServiceImplTest {
  
  @Mock
  RestTemplate restTemplate;
  
  @Mock
  HttpHeaders httpHeaders;
  
  @Mock
  ResponseEntity responseEntity;
  
  @InjectMocks
  PartnerFulfillmentServiceImpl partnerFulfillmentService;
  
  @Test
  public void createAppointmentTest() {
  
      Whitebox.setInternalState(partnerFulfillmentService, "internalServiceUrl", "http://localhost:8080");
  
      AppointmentRequest appointmentRequest = new AppointmentRequest();
      appointmentRequest.setPartnerName("CENTRICITY");
      appointmentRequest.setTicketNumber("123ABC");
  
      httpHeaders = new HttpHeaders();
      httpHeaders.set("Content-type", "application/json");
  
      responseEntity = new ResponseEntity&lt;&gt;(
              "some response body",
              HttpStatus.OK
      );
  
      when(restTemplate.exchange(Mockito.anyString(),
              Mockito.&lt;HttpMethod&gt; any(),
              Mockito.&lt;HttpEntity&lt;?&gt;&gt; any(),
              Mockito.&lt;Class&lt;Object&gt;&gt; any()))
              .thenReturn(responseEntity);
  
      ResponseEntity&lt;AppointmentResponse&gt; response = partnerFulfillmentService.createAppointment(appointmentRequest);
  
      Assert.assertEquals(response.getStatusCode(), HttpStatus.OK);
  }
  }
  </code></pre>
  
  <p>I'm getting 
  java.lang.AssertionError: 
  Expected :500
  Actual   :200
   and understandably so because it is not actually calling running <code>.thenReturn(responseEntity);</code> logic. My million dollar question is, why? It should be returning the responseEntity value. I have all arguments for the exchange() to any() in hopes to trigger the condition as often as possible as I can always narrow the conditions at a different time. Am I not mocking my restTemplate correctly? That is my current suspicion as to what is going on. Any advice would help!</p>
  
  <p>Thanks!</p>