我有这个 RestTemplate,我想模拟它并返回一个基于 json 文件的对象
ResponseEntity<List<Hotel>> deliveryResponse =
restTemplate.exchange(link.getHref(),
HttpMethod.GET, null, new ParameterizedTypeReference<List<Hotel>>() {
});
我尝试的模拟:
when(restTemplate.exchange(eq("delivery"), eq(HttpMethod.GET), any(), eq(Object.class)))
.thenReturn(readObjectFromFile("hotel.json", Order.class));
和
private <T> T readObjectFromFile(final String fileName, final Class<T> clazz) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(this.getClass().getClassLoader().getResourceAsStream("__files/" + fileName), clazz);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
但是我有这个编译错误:
Cannot resolve method 'thenReturn(T)'
我想你有错别字吗? 试试这个。
when(restTemplate.exchange(eq("delivery"), eq(HttpMethod.GET), any(), eq(Object.class)))
.thenReturn(readObjectFromFile("hotel.json"), Order.class);