问题
Trying the mocking as below :
Mockito.doReturn(responseEntity).when(restTemplate.exchange(anyString(), anyObject(), anyObject(), anyObject()));
this gave me a compilation issue:
"The method exchange(String, HttpMethod, HttpEntity, Class, Object[]) is ambiguous for the type RestTemplate"
so tried :
GetRelationshipInfoResponse relationship = getEntity();
ResponseEntity<GetRelationshipInfoResponse> responseEntity = new ResponseEntity<GetRelationshipInfoResponse>(relationship,
HttpStatus.ACCEPTED);
Mockito.doReturn(responseEntity).when(restTemplate.exchange(anyString(), Matchers.eq(HttpMethod.POST),
Matchers.<HttpEntity<?>> any(), Matchers.<Class<Object>> any()));
I see in runtime now in MethodInterceptorFilter
.intercept is getting null object value.
Can anybody suggest how can I fix.
回答1:
You shouldn't need to mock RestTemplate
s. There is a framework designed for this exact reason: @RestClientTest
See here:
http://www.baeldung.com/restclienttest-in-spring-boot
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/client/RestClientTest.html
来源:https://stackoverflow.com/questions/50271164/how-do-i-mock-resttemplate-exchange