Mockito not working for RestTemplate

Deadly 提交于 2019-12-12 02:44:42

问题


I am using mockito to mock a RestTemplate exchange call. Following is what I've used, but it's not picking up the mocked RestTemplate.

Mocked call.

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, userId);

The mocked RestTempate as follows.

Mockito.when(restTemplate.exchange(
            Matchers.anyString(),
            Matchers.any(HttpMethod.class),
            Matchers.<HttpEntity<?>> any(),
            Matchers.<Class<String>> any(),
            Matchers.anyString())).
            thenReturn(responseEntity);

Any idea what went wrong here? This runs with @RunWith(PowerMockRunner.class) Since I am mocking a static content.


回答1:


The signature has Object... as last param, so you have to use anyVarArg(). This is working fine here:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(MockitoJUnitRunner.class)
public class Foo {
    @Mock
    private RestTemplate restTemplate;

    @Test
    public void testXXX() throws Exception {
        Mockito.when(this.restTemplate.exchange(Matchers.anyString(), Matchers.any(HttpMethod.class), Matchers.any(), Matchers.<Class<String>>any(), Matchers.<Object>anyVararg()))
               .thenReturn(ResponseEntity.ok("foo"));

        final Bar bar = new Bar(this.restTemplate);
        assertThat(bar.foobar()).isEqualTo("foo");
    }

    class Bar {
        private final RestTemplate restTemplate;

        Bar(final RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
        }

        public String foobar() {
            final ResponseEntity<String> exchange = this.restTemplate.exchange("ffi", HttpMethod.GET, HttpEntity.EMPTY, String.class, 1, 2, 3);
            return exchange.getBody();
        }
    }
}

Note: the use of anyVarArg, a cast (Object) Matches.anyVarArgs() is also possible to avoid the ambiguous method error.




回答2:


Since I am doing the following in my method. The mocked exchange method is not binding.

RestTempate restTempalte = new RestTemplate();

So I change my source code to the following, and create a method to create a RestTempate instance.

public RestTemplate createRestTemplate() {
    return new RestTemplate();
}

And did the following in the test source.

@Before
public void createMyClass() {
    MockitoAnnotations.initMocks(this);

    sampleClient = new SampleClient() { 
        @Override
        public RestTemplate createRestTemplate() {
            return restTemplate;
        }
    };
}


来源:https://stackoverflow.com/questions/44104937/mockito-not-working-for-resttemplate

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