gpt4 book ai didi

java - Mockito when() 不区分子类

转载 作者:行者123 更新时间:2023-11-29 04:55:32 27 4
gpt4 key购买 nike

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

问题是在使用时Mockito.when(mockedClient.execute(any(HttpPost.class))).thenReturn(postResponse)Mockito.when(mockedClient.execute(any(HttpGet.class))).thenReturn (getResponse),其中响应是不同的对象,mockedClient.execute() 始终返回 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);
}
}

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

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

一如既往,我们感谢您提供的任何帮助。

最佳答案

尽管在 Mockito 1.x 中用英文阅读时是什么意思, any(HttpGet.class) matches any value and not just any HttpGet .该参数仅用于保存 Java 8 之前的转换。

来自Matchers.any(Class) documentation :

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) future 更改的评论匹配器。

关于java - Mockito when() 不区分子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33904877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com