【发布时间】:2017-05-22 04:59:48
【问题描述】:
我正在使用 mockito 来模拟 RestTemplate 交换调用。以下是我使用过的,但它没有选择模拟的 RestTemplate。
模拟调用。
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, userId);
模拟的 RestTempate 如下。
Mockito.when(restTemplate.exchange(
Matchers.anyString(),
Matchers.any(HttpMethod.class),
Matchers.<HttpEntity<?>> any(),
Matchers.<Class<String>> any(),
Matchers.anyString())).
thenReturn(responseEntity);
知道这里出了什么问题吗?这与 @RunWith(PowerMockRunner.class) 一起运行,因为我正在模拟静态内容。
【问题讨论】:
-
@RC。也尝试使用 Matchers.
anyVararg()。仍然没有接听模拟电话。 -
然后,我收到重载解析失败错误。不能'编译代码。 (对交换的引用不明确)因为所有参数都相同,并且只有最后一个参数是对象 var arg 和 Map?
-
试试这个
exchange(anyString(), any(), any(), Mockito.<Class<?>> any(), anyString()) -
确保在执行 SUT 之前必须调用
Mockito.when。
标签: java mockito resttemplate