【问题标题】:Ambiguous Method Call Mocking RestTemplate.exchange()模棱两可的方法调用模拟 RestTemplate.exchange()
【发布时间】:2019-12-13 07:36:38
【问题描述】:

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

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

我尝试过使用 any(Class.class) 和其他一些东西,但没有任何效果。我试图区分两种具有相似签名的方法:

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

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

这是我目前与 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 的方法的调用?

【问题讨论】:

    标签: java rest spring-boot mockito


    【解决方案1】:

    您很可能正在使用when().then() 模式。

    尝试使用您的匹配器使用doReturn().when() 方法。

    只要确保您使用匹配器,即使是您直接期望的参数(为此使用 eq())。

    这是project issue tracker 上的问题并修复。

    【讨论】:

    • 虽然您提到的问题仍然存在,但在使用一些较新版本的 mockito 进行尝试时,似乎没有错误(不再?)。
    【解决方案2】:

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

    [我在您的示例中使用了 JUnit 4 而不是 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));
        }
    }
    

    【讨论】:

    • 这解决了我的问题。我将您的测试复制到我的一个测试类中并成功运行。但是,当我从前 3 个 any() 匹配器中的任何一个中删除类类型时,它会失败。对 any() 的每次调用都没有明确的类类型参数似乎是我问题的根源。
    • 如果您处理重载方法,请始终使用 any 指定参数类型。如果您不这样做,mockito 将无法识别匹配的方法签名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多