有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Mockito不适用于RestTemplate

我正在使用mockito模拟RestTemplate交换调用。下面是我所使用的,但它没有使用模拟的RestTemplate

模拟呼叫

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

嘲弄的复述如下

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

知道这里出了什么问题吗?由于我模拟的是静态内容,因此它使用@RunWith(PowerMockRunner.class)运行


共 (3) 个答案

  1. # 1 楼答案

    signatureObject...作为最后一个参数,因此必须使用anyVarArg()。这在这里工作得很好:

    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();
            }
        }
    }
    

    注意:使用anyVarArg和强制转换(Object) Matches.anyVarArgs()也可以避免不明确的方法错误

  2. # 2 楼答案

    因为我正在用我的方法做以下事情。模拟的交换方法没有绑定

    RestTempate restTempalte = new RestTemplate();
    

    因此,我将源代码更改为以下内容,并创建一个方法来创建RestTempate实例

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

    并在测试源中执行以下操作

    @Before
    public void createMyClass() {
        MockitoAnnotations.initMocks(this);
    
        sampleClient = new SampleClient() { 
            @Override
            public RestTemplate createRestTemplate() {
                return restTemplate;
            }
        };
    }
    
  3. # 3 楼答案

    我也在为同样的问题挣扎。这是一个对我有用的笑话

     when(this.restTemplate.exchange(anyString(),
                any(),
                any(),
                eq(String.class),
                anyString()))
                .thenReturn(new ResponseEntity<>(subscriptionDataJson,
                        HttpStatus.OK));
    

    注意,我使用varargs作为anyString()