I am working with Java, Spring's RestTemplate, and Mockito, using Eclipse. I am trying to mock Spring's rest template, and the last parameter for the method I am mocking is a Class type. Below is the signature for the function:
public <T> ResponseEntity<T> exchange(URI url,
HttpMethod method,
HttpEntity<?> requestEntity,
Class<T> responseType)
throws RestClientException
The initial attempt I made to mock this method is as follows:
//given restTemplate returns exception
when(restTemplate.exchange(isA(URI.class), eq(HttpMethod.POST), isA(HttpEntity.class), eq(Long.class))).thenThrow(new RestClientException(EXCEPTION_MESSAGE));
However, this line of code produces the following error from eclipse:
The method exchange(URI, HttpMethod, HttpEntity<?>, Class<T>) in the type RestTemplate is not applicable for the arguments (URI, HttpMethod, HttpEntity, Class<Long>)
Eclipse then suggests I cast the last parameter with a 'Class' cast, but does not seem to work if I cast it to a 'Class', or other type.
I've been looking online for help on this, but seem to stumped on the fact that the parameter requested is a class type.
The answers I've looked at so far have been mainly related to generic collections. Any help here would be greatly appreciated.
Figured out.
The method being called was a parameterized method, but could not infer the parameter type from the matcher argument (the last argument was of type Class).
Making the explicit call
when(restTemplate.<Long>exchange(isA(URI.class),eq(HttpMethod.POST),isA(HttpEntity.class), eq(Long.class))).thenThrow(new RestClientException(EXCEPTION_MESSAGE));
fixed my problem.