我正在为执行GET请求(对外部系统的查询)的方法之一编写单元测试用例,并接收存储在模型对象中的查询结果,但我无法模拟其余的模板交换.需要一些帮助.
以下代码包括我的方法以及该方法的测试类.
public Car getCarModelDetails(String id) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", APPLICATION_JSON);
HttpEntity entity = new HttpEntity<>("parameters", headers);
ResponseEntity 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 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 {
}
@Test
public void getCarModelDetails_valid() throws JSONException {
String id = null;
HttpEntity 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);
}