問題描述
我編寫了一個使用 Mockito 1.9.5 的測試.我有一個 HttpGet 和一個 HttpPost 其中一個 HttpClient 執(zhí)行,我正在測試以驗證每個響應是否以輸入流的形式返回預期結(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)
,其中響應是不同的對象,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.該參數(shù)僅用于保存 Java 8 之前的強制轉(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) 文檔:
匹配任何對象,包括空值
Matches any object, including nulls
此方法不對給定參數(shù)進行類型檢查,它只是為了避免在代碼中進行強制轉(zhuǎn)換.但是,這可能會在未來的主要版本中發(fā)生變化(可能會添加類型檢查).
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 關(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ū)分子類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!