Issue
In groovy I'm trying to use the following to mock a return of a request but I keep getting a null pointer exception whenever my code calls:
ResponseEntity<AnimalVO> result = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headerUtil.headers()), AnimalVO.class);
In Test:
when(restTemplate.exchange(anyString(), any(HttpMethod.class, any(HttpEntity.class, any(ValueObject.class) as Class)).thenReturn(responseEntityMocked)
I'm using mockito 3.12 My test just fails with a null pointer exception and my restTemplate exchange (upon debugging) returns a null value. Am I doing something wrong?
If it helps the rest Temple exchange has the following definition:
exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity.
Solution
Try the following:
when(restTemplate.exchange(anyString(), any(HttpMethod.class),
any(HttpEntity.class), eq(AnimalVO.class)).thenReturn(responseEntityMocked)
Answered By - João Dias