Mockito when() 不区分子类

2022-01-14 00:00:00 mockito java apache-httpcomponents

我编写了一个使用 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.

相关文章