java - Running AsyncHttpClient with JUNIT Tests -


i've got following junit test i've been trying run. when try print out userhash string, string null. suggests httpclient isn't running @ all. i've been following post...

https://github.com/loopj/android-async-http/issues/173

here class.

public class ecapimanagertests {     instrumentationtestcase runnerhelper = new instrumentationtestcase();     private final string useremail = "removed";     private final string password = "removed";     string userhash;       @test     public void testpost()throws throwable{         final asynchttpclient httpclient = new asynchttpclient();           runnerhelper.runtestonuithread(new runnable() {             @override             public void run() {                 requestparams params = new requestparams();                 params.put("email", useremail);                 params.put("password", password);                 httpclient.post("https://edu.chat/api/login/", params, new asynchttpresponsehandler() {                     @override                     public void onsuccess(int statuscode, header[] headers, byte[] responsebody) {                         //called when response code 200                         userhash = new string(responsebody);                     }                     @override                     public void onfailure(int statuscode, header[] headers, byte[] responsebody, throwable error) {                      }                 });               }         });          try {             system.out.println(userhash);             jsonobject obj = new jsonobject(userhash);             assert.assertequals("true", obj.getstring("success"));          } catch (jsonexception e) {             e.printstacktrace();         }          } 

your forgot countdownlatch part of solution. important because request asynchronous, need wait until request finishes before looking @ results. latch having calling thread wait until onfinish called response handler.

public class ecapimanagertests {     instrumentationtestcase runnerhelper = new instrumentationtestcase();     private final string useremail = "removed";     private final string password = "removed";     string userhash;      @test     public void testpost()throws throwable{         final asynchttpclient httpclient = new asynchttpclient();         final countdownlatch signal = new countdownlatch(1);          runnerhelper.runtestonuithread(new runnable() {             @override             public void run() {                 requestparams params = new requestparams();                 params.put("email", useremail);                 params.put("password", password);                 httpclient.post("https://edu.chat/api/login/", params, new asynchttpresponsehandler() {                     @override                     public void onsuccess(int statuscode, header[] headers, byte[] responsebody) {                         //called when response code 200                         userhash = new string(responsebody);                     }                     @override                     public void onfailure(int statuscode, header[] headers, byte[] responsebody, throwable error) {                      }                     @override                     public void onfinish() {                         signal.countdown();                     }                 });               }         });         try {             signal.await(30, timeunit.seconds); // wait callback         } catch (interruptedexception e) {             e.printstacktrace();         }         try {             system.out.println(userhash);             jsonobject obj = new jsonobject(userhash);             assert.assertequals("true", obj.getstring("success"));          } catch (jsonexception e) {             e.printstacktrace();         }         } 

Comments