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

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

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的此方法的调用?

回答:

我不确定是否误解了您的问题或提及的问题@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));

}

}

以上是 模棱两可的方法调用模拟RestTemplate.exchange() 的全部内容, 来源链接: utcz.com/qa/432224.html

回到顶部