【发布时间】:2016-07-29 17:14:43
【问题描述】:
我应该如何编写以下 Mockito Matchers 以使调用不模棱两可?
我试图在我的代码中模拟的实际函数调用是:
//Variables
String url = http://theServer:8080/oath2-v1/token;
HttpEntity<String> request = new HttpEntity<String>("name=value",headers);
//Method call I am trying to mock using Mockito
response=cmsRestTemplate.exchange(url, HttpMethod.POST, request, DdsOAuthToken.class);
下面是我的单元测试用例中的一个 sn-p。它包含以下模拟上述调用的模拟调用,但不幸的是编译器发现它不明确并且不会编译。
//From the Unit Test...
when(restTemplate.exchange(
Matchers.anyString(),
Matchers.any(HttpMethod.class),
Matchers.any(HttpEntity.class),
Matchers.<Class<DdsOAuthToken>>any(),
Matchers.anyVararg()).thenReturn(response));
我得到的错误如下:
The method exchange(String, HttpMethod, HttpEntity<?>, Class<DdsOAuthToken>, Object[]) is ambiguous for the type RestTemplate
这是一个 Spring RestTemplate api 调用。具体来说,它发现模棱两可的 2 个 api 调用是以下 2 个调用:
1. exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)
2. exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables)
我试图模拟上面的#1。但是 Java 编译器无法判断我是在尝试调用 #1 还是 #2。究竟我应该如何编写 Mockito 匹配器,以便它知道我想要上面的 #1 而不是 #2?
【问题讨论】:
标签: java unit-testing compiler-errors mockito matcher