时的Mockito()不子类区分 [英] Mockito when() doesn't differentiate between child classes

查看:386
本文介绍了时的Mockito()不子类区分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个使用1.9.5的Mockito测试。我有一个<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/HttpGet.html\"相对=nofollow> HTTPGET 和<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/HttpPost.html\"相对=nofollow> HttpPost 其中<一个href=\"https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/CloseableHttpClient.html\"相对=nofollow> 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(任何(HttpPost.class)))。thenReturn(postResponse) Mockito.when(mockedClient.execute(任(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);
}

一如往常,你可以提供任何帮助AP preciated。

As always, any help you can provide is appreciated.

推荐答案

尽管什么英文的,在1.x中的Mockito,的 任何(HttpGet.class)匹配的任意值的并不仅仅是 HTTPGET任何。该参数仅用于铸造previous保存到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.

匹配器.ANY(类)文档

匹配任何对象,包括空

此方法不执行类型检查用给定的参数,它是唯一的有,以避免在code铸造。然而,这可能会改变(可以添加类型检查)在将来的主要版本。

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)来代替,看布赖斯的评论如下关于对的任何未来的变化。(类&LT;&GT; 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()不子类区分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆