I have the stubbing for the exchange call on a Spring
RestTemplate
defined as follows:
Class<String> dummyResponseType = String.class;
ResponseEntity dummyResponse = mock(ResponseEntity.class);
given(dummyResponse.getBody()).willReturn("Hello world");
HttpHeaders responseHeaders = new HttpHeaders();
String returnedCorrelationId = UUID.randomUUID().toString();
responseHeaders.add(HttpHeaderConstants.CORRELATION_ID, returnedCorrelationId);
given(dummyResponse.getHeaders()).willReturn(responseHeaders);
given(restTemplate.exchange(anyString(), eq(HttpMethod.GET), any(RequestEntity.class), eq(dummyResponseType))).willReturn(dummyResponse);
To get a predefined response I accept any string, then the GET method, any request entity and a defined response type.
However I do get a PotentialStubbingProblem
:
org.mockito.exceptions.misusing.PotentialStubbingProblem:
Strict stubbing argument mismatch. Please check:
- this invocation of 'exchange' method:
restTemplate.exchange(
"http://localhost:8080//api/ping",
GET,
<java.lang.Object@3b152928,[X-Correlation-Id:"00e2ab7c-808e-4e1c-b3a9-d65eff7b37ca", Authorization:"Bearer authToken"]>,
class java.lang.String
);
-> at
- has following stubbing(s) with different arguments:
1. restTemplate.exchange("", null, null, null);
From the exception I see that the expected stubbing is not what I have defined and I cannot figure out why this happens. Doing lenient stubbing here instead does not solve the problem, as it still does not match. Even defining it like follows yields the same result:
given(restTemplate.exchange(anyString(), any(HttpMethod.class), any(RequestEntity.class), any(Class.class))).willReturn(dummyResponse);
The exchange method on the RestTemplate
is overloaded, but with the types being present in the stubbing, this should not pose a problem. What am I overlooking here?
The stubbing is defined wrong for the exchange
method on RestTemplate
.
The third parameter is not a RequestEntity
but a HttpEntity
containing the request. With this stubbing change the invocation is found.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.