I am having trouble testing Handler
code with Robolectric. For example:
public class Client {
private Handler mMainThreadHandler;
public interface Callback{
void ok();
}
public Client() {
mMainThreadHandler = new Handler(Looper.getMainLooper());
}
public void doSomeStuff(Callback callback){
//doing...
mMainThreadHandler.post(new Runnable(){
@Override public void run() {
callback.ok();
}
});
}
}
How do I run the code in the Runnable
immediately? It doesn't run before my test is done executing.
For Robolectric version 3.0 you should use:
org.robolectric.Robolectric.flushForegroundThreadScheduler
or org.robolectric.shadows.ShadowLooper.runUiThreadTasks
org.robolectric.shadows.ShadowLooper.runUiThreadTasksIncludingDelayedTasks
Reference: 2.4 to 3.0 Upgrade Guide
I think this should make a job:
Robolectric.runUiThreadTasks();
or if there several tasks scheduled:
Robolectric.runUiThreadTasksIncludingDelayedTasks();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With