java capture http_java-响应实体执行HTTP GET请求的模拟单元测...

我正在为执行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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值