darmbre
darmbre

Reputation: 116

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?

Upvotes: 6

Views: 6257

Answers (2)

SpaceTrucker
SpaceTrucker

Reputation: 13556

You have to add a cast (Object[]) to the vararg parameter. This may have something to do with the declaration of the anyVararg method. But I'm not sure about that. So your code should be:

//From the Unit Test...
when(restTemplate.exchange(
    Matchers.anyString(),
    Matchers.any(HttpMethod.class),
    Matchers.any(HttpEntity.class),
    Matchers.<Class<DdsOAuthToken>>any(),
    (Object[]) Matchers.anyVararg()).thenReturn(response));

Upvotes: 3

Nazarii Bardiuk
Nazarii Bardiuk

Reputation: 4342

Usually it is a bad decision to mock classes that you do not control. Spring framework comes with utility classes that help you to test your usage of framework.

For example MockRestServiceServer is a dummy server that will give valid responses for RestTemplate instance so you do not need to mock it.

An example from docs

RestTemplate restTemplate = new RestTemplate();

MockRestServiceServer mockServer =  MockRestServiceServer.createServer(restTemplate);

mockServer.expect(requestTo("/greeting"))
      .andRespond(withSuccess("Hello world", "text/plain"));

// use RestTemplate ...

Upvotes: 0

Related Questions