java - Android JUnit: assert inside the handler -
i trying make assert inside of handler without success. tried execute threads, runnables, etc. next code 1 example:
public class servertest extends androidtestcase { private final string tag = this.getclass().getsimplename(); private final int ack = 1; public void testsendtoserver(){ final serverstub server = new serverstub(); final countdownlatch signal = new countdownlatch(1); new thread(new runnable() { handler handler = new handler(new handler.callback() { @override public boolean handlemessage(message msg) { // log never appear log.w(tag, "handlemessage(message "+msg+")"); assertequals(ack, msg.what); signal.countdown(); return true; } }); @override public void run() { // log never appear log.w(tag, "senttoserver()"); server.senttoserver(handler); } }).start(); try { signal.await(15, timeunit.seconds); } catch (interruptedexception e) { e.printstacktrace(); } //this assert fails assertequals(0, signal.getcount()); } class serverstub{ private final string tag = this.getclass().getsimplename(); public void senttoserver(handler handler){ message message = new message(); message.what = ack; log.i(tag, "ack_received"); handler.sendmessage(message); } } }
i want test method class doesn't return nothing, "do something" , return response using handler.
i execute test case testsendtoserver()
next intention:
in test serverstub created has method senttoserver(handler handler)
. in method send "ack" message handler. after create countdownlatch signal = new countdownlatch(1);
call signal.countdown();
inside handler handlemessage(message msg)
method know if executed.
continue test, create new thread handler created , senttoserver(handler handler)
called. inside handler assertequals(ack, msg.what);
called need know if serverstub sent me "ack".
main thread waiting 15 seconds or signal.countdown();
executed. call assertequals(0, signal.getcount());
check if handler executed.
however handler handlemessage(message msg)
method never executed.
i read other posts , tried lot of examples, never found answer. possible that?
more information: trying create handler inside thread, this:
new thread() { @override public void run() { log.w(tag, "thread run"); handler handler = new handler(new handler.callback() { ... } } }.start();
the log "thread run" printed after "test run failed: instrumentation run failed due 'java.lang.runtimeexception'".
Comments
Post a Comment