Java 模棱两可的方法调用Mocking restemplate.exchange()

Java 模棱两可的方法调用Mocking restemplate.exchange(),java,rest,spring-boot,mockito,Java,Rest,Spring Boot,Mockito,无法找到正确的方法来使用匹配器来识别我针对的是哪个exchange方法重载。我正在打的电话: restemplate.exchange(url、HttpMethod.PUT、httpEntity、Object.class) 我尝试过使用任何(Class.Class)和其他一些东西,但都不起作用。我试图区分两种具有类似签名的方法: exchange(字符串url,HttpMethod方法,@Nullable-HttpEntity-requestEntity,Class-responseType)

无法找到正确的方法来使用匹配器来识别我针对的是哪个exchange方法重载。我正在打的电话:

restemplate.exchange(url、HttpMethod.PUT、httpEntity、Object.class)

我尝试过使用任何(Class.Class)和其他一些东西,但都不起作用。我试图区分两种具有类似签名的方法:

exchange(字符串url,HttpMethod方法,@Nullable-HttpEntity-requestEntity,Class-responseType)

exchange(字符串var1、httpmethodvar2、@Nullable HttpEntity var3、参数化类型引用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.*;

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

很可能您使用的是
when().then()
模式

与您的匹配者一起尝试
doReturn().when()
方法

只需确保即使对于直接期望的参数也使用匹配器(使用eq()即可)


以下是问题和解决方法。

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

[我使用JUnit4作为示例,而不是JUnit5]

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));
    }
}
import static org.mockito.ArgumentMatchers.any;
导入org.junit.Assert;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.mockito.mockito;
导入org.mockito.junit.MockitoJUnitRunner;
导入org.springframework.core.ParameterizedTypeReference;
导入org.springframework.http.HttpEntity;
导入org.springframework.http.HttpMethod;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.client.rest模板;
@RunWith(MockitoJUnitRunner.class)
公共类模拟测试{
@试验
公开无效测试(){
RestTemplateAPI=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)),然后返回(response1);
Mockito.when(api.exchange(any(String.class)、any(HttpMethod.class)、any(HttpEntity.class)、any(ParameterizedTypeReference.class)),然后返回(response2);
ParameteredTypeReference mock=Mockito.mock(ParameteredTypeReference.class);
Assert.assertEquals(response1,api.exchange(“”,HttpMethod.GET,new-HttpEntity(“”,String.class));
Assert.assertEquals(response2,api.exchange(“”,HttpMethod.GET,新的HttpEntity(“”,mock));
}
}

虽然您提到的问题仍然存在,但在使用一些较新版本的mockito进行测试时,似乎没有出现错误(不再出现了?),这解决了我的问题。我将您的测试复制到我的一个测试类中并成功运行了它。但是,当我从前3个any()匹配器中的任何一个移除类类型时,它会失败。每次调用any()都没有显式的类类型参数似乎是我的问题的根源。如果处理重载方法,请始终使用any来指定参数类型。如果不这样做,
mockito
将无法识别匹配的方法签名。