简体   繁体   中英

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. Need to mock function getPdf. I am unable to mock GetDocumentRequest and GetDocumentResponse . Mockito or PowerMockito I need to use for mocking. 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. 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM