模棱两可的方法调用模拟 RestTemplate.exchange()

2022-01-14 00:00:00 rest mockito java spring-boot

无法找出使用匹配器来识别我所针对的交换方法的重载的正确方法.我正在拨打的电话:

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)

我尝试过使用 any(Class.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 方法, @Nullable HttpEntity<?> requestEntity, Class<T> responseType)

exchange(String var1, HttpMethod var2, @Nullable HttpEntity

这是我目前与 Mockito 相关的导入:

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.*;

是否有人能够模拟对使用 Class 作为第四个参数而不是 ParameterizedTypeReference 的方法的调用?

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

推荐答案

我不确定是我误解了你的问题还是 @MarciejKowalski 提到的问题,但是从问题运行测试时或我认为与您针对 mockito-core-2.23.4/JDK 1.8.0_151 的示例类似,它工作得很好.

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.

[我在您的示例中使用了 JUnit 4 而不是 JUnit 5]

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

相关文章