vendredi 29 juillet 2016

Mockito Unit Test Case call is Ambiguous (Need to get it to not be ambiguous)

How should I write the following Mockito Matchers so that the call is not ambiguous?

The actual function call I am trying to mock in my code is:

//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);

Below is a snippet from my Unit Test case. It contains the following mocked call emulating the above call, but unfortunately the compiler finds it ambiguous and won't compile.

//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 error I get is as follows:

The method exchange(String, HttpMethod, HttpEntity<?>, Class<DdsOAuthToken>, Object[]) is ambiguous for the type RestTemplate

This is a Spring RestTemplate api call. Specifically the 2 api calls it finds ambiguous are the 2 following calls:

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)

I am trying to mock #1 above. But the Java Compiler can't tell if I'm trying to call #1 or #2. Exactly how should I write the Mockito matchers so that it knows I want #1 above and not #2?

Aucun commentaire:

Enregistrer un commentaire