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

时间:2023-05-02
本文介绍了模棱两可的方法调用模拟 RestTemplate.exchange()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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

这篇关于模棱两可的方法调用模拟 RestTemplate.exchange()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!