mockito unit test cases for Response entity performing HTTP GET request

Aarika :

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.

The below code includes my method and also my test class for the method.

public Car getCarModelDetails(String id) {
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", APPLICATION_JSON);

    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
    ResponseEntity<QueryResultCar> 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<Car> records = exchange.getBody().records;
        if (records != null && records.size() == 1) {
            return records.get(0);
        } else (records == null || records.isEmpty()) {
            return null;
        } 

    } else {
        throw new RuntimeException();
    }
}


private static class QueryResultCar extends QueryResult<Car> {
}

  @Test
public void getCarModelDetails_valid() throws JSONException {   
    String id = null;
    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
     new ResponseEntity<>("", 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);  

}
DCTID :

You need to use matchers and probably need to use verify and and arg captor to check all the things you want. I would probably divide this test up because it has many assertions, but this should get you started.

@RunWith(MockitoJUnitRunner.class)
public class SubjectTest {

@InjectMocks
private CarCar subject;
@Mock
private RestTemplate restTemplate;

@Test
public void getCarModelDetails_valid() throws JSONException {
    String id = "123";
    Config config = new Config();
    when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), any(HttpEntity.class), eq(QueryResultCar.class)))
            .thenReturn(new ResponseEntity<>(new QueryResultCar(), HttpStatus.OK));

    Car actual = subject.getCarModelDetails(id);

    ArgumentCaptor<HttpEntity> httpEntityArgumentCaptor = ArgumentCaptor.forClass(HttpEntity.class);
    verify(restTemplate).exchange(eq(config.restUrl + "/v" + config.restVersion + "/query?q=SELECT + SELECT_COLUMNS"
            + " FROM Car WHERE (Model = '" + id + "')"), eq(HttpMethod.GET), httpEntityArgumentCaptor.capture(), eq(QueryResultCar.class));
    assertEquals(APPLICATION_JSON_VALUE, httpEntityArgumentCaptor.getValue().getHeaders().get("Accept").get(0));
    assertEquals("Car to string", actual.toString());
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=75851&siteId=1