Mockito when() 不区分子类

Mockito when() doesn#39;t differentiate between child classes(Mockito when() 不区分子类)
本文介绍了Mockito when() 不区分子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我编写了一个使用 Mockito 1.9.5 的测试.我有一个 HttpGet 和一个 HttpPost 其中一个 HttpClient 执行,我正在测试以验证每个响应是否以输入流的形式返回预期结果.

I wrote a test that uses Mockito 1.9.5. I have an HttpGet and an HttpPost which an HttpClient execute, and I'm testing to verify that the response from each returns the expected result in the form of an input stream.

问题是在使用Mockito.when(mockedClient.execute(any(HttpPost.class))).thenReturn(postResponse)Mockito.when(mockedClient.execute(any(HttpGet.class))).thenReturn(getResponse),其中响应是不同的对象,mockedClient.execute() 总是返回 getResponse.

The issue is that while using Mockito.when(mockedClient.execute(any(HttpPost.class))).thenReturn(postResponse) and Mockito.when(mockedClient.execute(any(HttpGet.class))).thenReturn(getResponse), where the responses are different objects, mockedClient.execute() always returns getResponse.

我写的测试如下:

private InputStream       postContent;
private InputStream       getContent;
@Mock
private FilesHandler hand;
@Mock
private MockHttpClient    client;
private MockHttpEntity    postEntity;
private MockHttpEntity    getEntity;
private MockHttpResponse  postResponse;
private MockHttpResponse  getResponse;
private String imgQuery = "someQuery";
private ParametersHandler ph;
private FileHandlerImpl   fileHand;
private String            indexFolder = "src/test/resources/misc/";
private String            indexFile   = "index.csv";

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    try {
        postContent = new FileInputStream(indexFolder + "testContent.txt");
        postEntity = new MockHttpEntity(postContent);
        postResponse = new MockHttpResponse(postEntity, new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "postReasonPhrase"));
        getContent = new FileInputStream(indexFolder + "testContent.txt");
        getEntity = new MockHttpEntity(getContent);
        getResponse = new MockHttpResponse(getEntity, new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "getReasonPhrase"));
        ph = new ParametersHandler();
        fileHand = new FileHandlerImpl(client, ph, indexFolder, indexFile);
    } catch (Exception e) {
        failTest(e);
    }
}
@Test
public void getFileWhenEverythingJustWorks() {

    try {
        Mockito.when(client.execute(Mockito.any(HttpPost.class))).thenReturn(postResponse);
        Mockito.when(client.execute(Mockito.any(HttpGet.class))).thenReturn(getResponse);
        fileHand.getFile(hand, imgQuery, ph, "I");
        Mockito.verify(hand).rebuildIndex(Mockito.any(String.class), Mockito.any(Map.class), Mockito.any(Map.class), hand);
    } catch (IOException e) {
        failTest(e);
    }
}

正在测试的方法的简化版本如下.

A shortened version of the method being tested is below.

public void getFile(FilesHandler fileHandlerFl, String query, ParametersHandler ph, String type) {

        JsonFactory factory = new JsonFactory();
        HttpPost post = preparePost(query, factory);
        CloseableHttpResponse response = client.execute(post);

    String uri = "https://someuri.com" + "/File";
        HttpGet get = new HttpGet(uri);
        setParameters(get);
        response.close();
        response = client.execute(get);
}

一如既往,感谢您提供的任何帮助.

As always, any help you can provide is appreciated.

推荐答案

尽管在 Mockito 1.x 中用英语阅读时它意味着什么,any(HttpGet.class) 匹配 any value 而不仅仅是 any HttpGet.该参数仅用于保存 Java 8 之前的强制转换.

Despite what it would mean when read in English, in Mockito 1.x, any(HttpGet.class) matches any value and not just any HttpGet. The parameter is only used to save a cast previous to Java 8.

来自 Matchers.any(Class) 文档:

匹配任何对象,包括空值

Matches any object, including nulls

此方法不对给定参数进行类型检查,它只是为了避免在代码中进行强制转换.但是,这可能会在未来的主要版本中发生变化(可能会添加类型检查).

This method doesn't do type checks with the given parameter, it is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.

使用 isA(HttpGet.class)isA(HttpPost.class) 代替,请参阅下面 Brice 关于 any(Class<?> clazz) 匹配器.

Use isA(HttpGet.class) and isA(HttpPost.class) instead, and see Brice's comment below about future changes to the any(Class<?> clazz) matcher.

这篇关于Mockito when() 不区分子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

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 - 获取键盘按键)