title: mockito unit test cases for Response entity performing HTTP GET request
body: <p>I am writing unit test cases for one of my methods which performs GET Request(query to external system), and receives query results which i store in my model object, I am not able to mock the rest template exchange. Need some help with it.</p>
  
  <p>The below code includes my method and also my test class for the method.</p>
  
  <pre><code>public Car getCarModelDetails(String id) {
      HttpHeaders headers = new HttpHeaders();
      headers.set("Accept", APPLICATION_JSON);
  
      HttpEntity&lt;String&gt; entity = new HttpEntity&lt;&gt;("parameters", headers);
      ResponseEntity&lt;QueryResultCar&gt; exchange = restTemplate.exchange(
              config.restUrl + "/v" + config.restVersion + /query?q=SELECT + SELECT_COLUMNS
                      + " FROM Car WHERE (Model = '" + id + "')",
              HttpMethod.GET, entity, QueryResultCar.class);
  
      if (exchange.getStatusCode().equals(HttpStatus.OK)) {
          List&lt;Car&gt; records = exchange.getBody().records;
          if (records != null &amp;&amp; records.size() == 1) {
              return records.get(0);
          } else (records == null || records.isEmpty()) {
              return null;
          } 
  
      } else {
          throw new RuntimeException();
      }
  }
  
  
  private static class QueryResultCar extends QueryResult&lt;Car&gt; {
  }
  
    @Test
  public void getCarModelDetails_valid() throws JSONException {   
      String id = null;
      HttpEntity&lt;String&gt; entity = new HttpEntity&lt;&gt;("parameters", headers);
       new ResponseEntity&lt;&gt;("", HttpStatus.OK);
      Mockito.when(restTemplate.exchange(config.restUrl + "/v" + config.restVersion + /query?q=SELECT + SELECT_COLUMNS
                      + " FROM Car WHERE (Model = '" + id + "'), HttpMethod.GET, entity, QueryResultCar.class))
              .thenReturn(response);  
  
  }
  </code></pre>