I had the same problem (at least 2 involving this problem) and after searching for an plausible answer, I could fix it successful.
Before my explanation, I'll share 2 explanations that helped me. How do I mock a REST template exchange? and Mocking RestTemplate.exchange() method gives null value.
So, basically, the point is that every test case involving any RestTemplate's method has to give every argument correctly. If it is not ok, than Mockito fails and throws an exception like that:
org.mockito.exceptions.misusing.PotentialStubbingProblem
or with a NullPointerException getting ResponseEntity from restTemplate.exchange (it is your case)
Before (I'm hidding some implementations. Mockito threw an exception)
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(),
ArgumentMatchers.<ParameterizedTypeReference<FilterBean>>any())).thenReturn(responseEntityFilterBean);
Mockito exception
org.mockito.exceptions.misusing.PotentialStubbingProblem: Strict
stubbing argument mismatch. Please check:
- this invocation of 'exchange' method:
restTemplate.exchange(
"/filter",
GET,
<[Content-Type:"application/json"]>,
class br.com.test.bean.FilterBean );
-> at br.com.test.service.FilterService.getFilterStatus(FiltroService.java:60)
- has following stubbing(s) with different arguments:
- restTemplate.exchange("", null, null, null);
It's indicating that arguments aren't ok!
Changing my code it fixed my problem.
when(restTemplate.exchange(ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.any(),
ArgumentMatchers.<Class<FilterBean>>any())).thenReturn(responseEntityFilterBean);
In your case, try to do this and It'll solve:
when(restTemplate.exchange(ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.any(), ArgumentMatchers.<Class<List<String>>>any())))
.thenReturn(mockResponse);