I am trying to understanding how the generics work in case of Mockito.
I am trying to mock the RestTemplate
, however the following code does not compile and I don't know why:
var expectedResponse = createServiceResponse();
when(restTemplate.exchange(anyString(), any(HttpMethod.class),
Mockito.<HttpEntity<Object>>any(),
Mockito.<ServiceResponse>any().getClass(), anyMap()))
.thenReturn(ResponseEntity.ok(expectedResponse));
The compiler keeps giving me the error message:
Cannot resolve method 'thenReturn(org.springframework.http.ResponseEntity<T>)
But when I switch Mockito.<ServiceResponse>any().getClass()
with Mockito.<Class<ServiceResponse>>any()
the compilation succeeds.
Why is this happening ?
What is the difference between Mockito.<ServiceResponse>any().getClass()
and Mockito.<Class<ServiceResponse>>any()
?
They should be the same thing right ?
I mean the class of ServiceResponse
should be Class<ServiceResponse>
. Right ?
I don't have a good grasp of advanced generics so I can't understand what the compiler is doing.
Please do add some description in the answer so that I can understand why this is working the way it is suppose to.