Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Can't figure out the correct way to use matchers to identify which overload of the exchange method I am targetting. The call I am making:

restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Object.class)

I've tried using any(Class.class), and a couple other things but nothing is working. There are 2 methods with a similar signature that I am trying to distinguish between:

exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType)

exchange(String var1, HttpMethod var2, @Nullable HttpEntity<?> var3, ParameterizedTypeReference<T> var4)

Here are my current imports related to Mockito:

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

Has anyone been able to mock a call to this method that uses a Class as the 4th parameter instead of a ParameterizedTypeReference?

I am not sure whether I misunderstood your question or the issue mentioned by @MarciejKowalski, but when running the test from the issue or what I suppose is similar to your example against mockito-core-2.23.4 / JDK 1.8.0_151 it works just fine.

[I used JUnit 4 for your example instead of JUnit 5]

import static org.mockito.ArgumentMatchers.any;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
@RunWith(MockitoJUnitRunner.class)
public class MockitoTest {
    @Test
    public void test() {
        RestTemplate api = Mockito.mock(RestTemplate.class);
        ResponseEntity<?> response1 = Mockito.mock(ResponseEntity.class);
        ResponseEntity<?> response2 = Mockito.mock(ResponseEntity.class);
        Mockito.when(api.exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(Class.class))).thenReturn(response1);
        Mockito.when(api.exchange(any(String.class), any(HttpMethod.class), any(HttpEntity.class), any(ParameterizedTypeReference.class))).thenReturn(response2);
        ParameterizedTypeReference mock = Mockito.mock(ParameterizedTypeReference.class);
        Assert.assertEquals(response1, api.exchange("", HttpMethod.GET, new HttpEntity(""), String.class));
        Assert.assertEquals(response2, api.exchange("", HttpMethod.GET, new HttpEntity(""), mock));
                This solved my issue. I copied your test into one of my test classes and ran it successfully. However, When I remove the class type from any of the first 3 any() matchers it fails. Not having explicit class type parameters for each call to any() seems to have been the root of my issue.
– matthenry87
                Aug 5, 2019 at 23:00
                If you deal with overloaded methods always use the any with specificing the parameter type. In case you do not do that mockito is unable to identify the matching method signature.
– second
                Aug 6, 2019 at 9:02

Most likely you are using the when().then() pattern.

Try the doReturn().when() approach with your matchers.

Just make sure you use matchers even for the params that you expect directly (use eq() for this).

Here is the issue and fix on project issue tracker.

While the issue you mentioned is still open, when trying it out with some newer versions of mockito there doesn't seem to be an error (anymore?). – second Aug 5, 2019 at 20:16

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.