How do i mock RestTemplate exchange

我的未来我决定 提交于 2020-01-16 01:16:09

问题


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 RestTemplates. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!