I'm trying to mock the restTemplate.exchange method of Spring Rest.
In the same test I have multiple calls which differ only by the return type.
Here are the methods with the mocks I created
First
// Original method restTemplate.exchange(UrlMap.SEARCH + '?' + searchDocsForm.toQueryParams(), HttpMethod.GET, null, new ParameterizedTypeReference<SearchResultsDTO<SolrDocumentDTO>>() { }) // Mock when(restTemplate.exchange(any(String.class), any(HttpMethod.class), any(), Matchers.<ParameterizedTypeReference<SearchResultsDTO<SolrDocumentDTO>>>any())).thenReturn( new ResponseEntity<>(searchResultsDTO, HttpStatus.OK)); Second
// Original method restTemplate.exchange(UrlMap.ALL_DOCUS_TOPICS, HttpMethod.GET, null, new ParameterizedTypeReference<List<SelectItem>>() { }).getBody(); // Mock when(restTemplate.exchange(any(String.class), any(HttpMethod.class), any(), Matchers.<ParameterizedTypeReference<List<SelectItem>>>any())).thenReturn( new ResponseEntity<>(selectItems, HttpStatus.OK)); The generic parameters of ParameterizedTypeReference are not considered by the mock, and the last defined mock wins over the former.
Is there any way to make it work?
any(String.class)withanyString(), and similarly foranyInt(),anyFloat(), etc... It's simpler, more concise, easire on the eyes, and not subject to type-erasure.