問題描述
我編寫了一個(gè)使用 Mockito 1.9.5 的測(cè)試.我有一個(gè) HttpGet 和一個(gè) HttpPost 其中一個(gè) HttpClient 執(zhí)行,我正在測(cè)試以驗(yàn)證每個(gè)響應(yīng)是否以輸入流的形式返回預(yù)期結(jié)果.
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)
,其中響應(yīng)是不同的對(duì)象,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
.
我寫的測(cè)試如下:
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);
}
}
正在測(cè)試的方法的簡(jiǎn)化版本如下.
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 中用英語閱讀時(shí)它意味著什么,any(HttpGet.class)
匹配 any value 而不僅僅是 any HttpGet.該參數(shù)僅用于保存 Java 8 之前的強(qiáng)制轉(zhuǎn)換.
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) 文檔:
匹配任何對(duì)象,包括空值
Matches any object, including nulls
此方法不對(duì)給定參數(shù)進(jìn)行類型檢查,它只是為了避免在代碼中進(jìn)行強(qiáng)制轉(zhuǎn)換.但是,這可能會(huì)在未來的主要版本中發(fā)生變化(可能會(huì)添加類型檢查).
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)
代替,請(qǐng)參閱下面 Brice 關(guān)于 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.
這篇關(guān)于Mockito when() 不區(qū)分子類的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!