Mocking a REST call with MockRestServiceServer
Seems that you are trying to test the rest-client, the rest-server should be tested in other place. You are using RestTemplate -> To call the service. Then, tried to mock RestTemplate and its call's results.
@Mock
RestTemplate restTemplateMock;
and Service Under Test Class
@InjectMocks
Service service;
Let say, Service has a method to be test as
public void filterData() {
MyResponseModel response = restTemplate.getForObject(serviceURL, MyResponseModel.class);
// further processing with response
}
Then, to test filterData method, you need to mock the response from restTemplate call such as
mockResponseModel = createMockResponse();
Mockito.when(restTemplateMock.getForObject(serviceURL, MyResponseModel.class)).thenReturn(mockResponseModel);
service.filterData();
//Other assert/verify,... go here
When you create an instance of MockRestServiceServer
you should use existing instance of RestTemplate
that is being used by your production code. So try to inject RestTemplate
into your test and use it when invoking MockRestServiceServer.createServer
- don't create new RestTemplate
in your tests.