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

Ambiguous Method Call Mocking RestTemplate.exchange()(模棱两可的方法调用模拟 RestTemplate.exchange())
本文介绍了模棱两可的方法调用模拟 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)