Mockito when() 不区分子 类
Mockito when() doesn't differentiate between child classes
我写了一个使用 Mockito 1.9.5 的测试。我有一个 HttpGet and an HttpPost which an HttpClient 执行,我正在测试以验证来自每个 returns 的响应是否为输入流形式的预期结果。
问题是在使用
Mockito.when(mockedClient.execute(any(HttpPost.class))).thenReturn(postResponse)
和 Mockito.when(mockedClient.execute(any(HttpGet.class))).thenReturn(getResponse)
,其中响应是不同的对象,mockedClient.execute()
总是 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);
}
}
正在测试的方法的简化版本如下。
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、。该参数仅用于保存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)
匹配器未来更改的评论。
我写了一个使用 Mockito 1.9.5 的测试。我有一个 HttpGet and an HttpPost which an HttpClient 执行,我正在测试以验证来自每个 returns 的响应是否为输入流形式的预期结果。
问题是在使用
Mockito.when(mockedClient.execute(any(HttpPost.class))).thenReturn(postResponse)
和 Mockito.when(mockedClient.execute(any(HttpGet.class))).thenReturn(getResponse)
,其中响应是不同的对象,mockedClient.execute()
总是 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);
}
}
正在测试的方法的简化版本如下。
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、。该参数仅用于保存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)
匹配器未来更改的评论。