android - How would you write a test for this method using Mockito? -


please assist in this. can't seem create suitable test method:

protected void startinterfacing() {      mliveauthclient.login(mview.context(), arrays.aslist(scopes), new liveauthlistener() {          @override         public void onauthcomplete(final livestatus livestatus, final liveconnectsession liveconnectsession,                                    object o) {              // login successful , user consented, retrieve user id , connect backend server             getuseridandconnectwithbackendserver(liveconnectsession, mliveauthclient);         }          @override         public void onautherror(liveauthexception e, object o) {             // failed authenticate auth service... show error             if (e.geterror().equals("access_denied") ||                     e.getmessage().equals("the user cancelled login operation.")) {                  // when user cancels in either login or consent page, need log user out enable                 // login screen again when trying connect later on                 loguserout(mliveauthclient, false);             } else {                 onerroroccured();             }         }     }); } 

i'll explain abit goes on here: i'm trying authenticate client , log onedrive. method starts call live sdk's login method. sdk object given me outside class. can mock it. here's i'm struggling with:

  1. i not need test call login method because not mine. need test call getuseridandconnectwithbackendserver() inside onauthcomplete. method requires liveconnectsession object. how provide that? given me on onauthcomplete method.

  2. how mock calls onauthcomplete , onautherror? read argumentcaptor when use that, need provide arguments methods when call actual method. instance, argument.getvalue().onauthcomplete() requires me add arguments call. provide here?

here next method same has own issues:

 protected void getuseridandconnectwithbackendserver(final liveconnectsession liveconnectsession, final liveauthclient         authclient) {      final liveconnectclient connectclient =  new liveconnectclient(liveconnectsession);      connectclient.getasync("me", new liveoperationlistener() {         @override         public void oncomplete(liveoperation liveoperation) {              // got result. check errors...             jsonobject result = liveoperation.getresult();             if (result.has(error)) {                 jsonobject error = result.optjsonobject(error);                 string code = error.optstring(code);                 string message = error.optstring(message);                 onerroroccured();             } else {                 connectwithbackend(result, liveconnectsession, authclient);             }         }          @override         public void onerror(liveoperationexception e, liveoperation liveoperation) {             // failed retrieve user information.... show error             onerroroccured();             loguserout(authclient, false);         }     }); } 

in here mock jsonobject instance. how call oncomplete method, or onerror method. , provide arguments methods provide me with. liveoperation instance?

thank you!!

the solution used use mockito's doanswer() structure. enabled me callback argument , call 1 of methods. solution use argumentcator.


Comments