[英]Mocking class object having setter getter methods present in function using mockito
I have a requirement in which I need to get some response from webservice. 我有一个要求,我需要从Web服务获得一些响应。 Need to mock function getPdf. 需要模拟功能getPdf。 I am unable to mock GetDocumentRequest
and GetDocumentResponse
. 我无法模拟GetDocumentRequest
和GetDocumentResponse
。 Mockito or PowerMockito I need to use for mocking. 我需要使用Mockito或PowerMockito进行模拟。 Eg: 例如:
Class xyz {
// mocking required.
String getPdf (int I, String h){
return getDoc(I, h):
}
String getDoc (int I, String h){
GetDocumentRequest d = factory.getDocument ():
d.setversion (I);
d.setname (h):
GetDocumentResponse r = getService ().getPdfDoc (d):
// webservice
return r.getPdfString ();
}
}
Mockito should be enough for mocking a normal method. Mockito应该足以模拟普通方法。 Maybe something similar to this? 也许与此类似?
@Test
public void test() {
Xyz xyz = mock(Xyz.class);
when(xyz.getPdf(any(Integer.class), any(String.class)).thenReturn("this is my pdf");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.