I'm trying to write some tests for an Android application where we are integrating Bolts.
The object under test works by creating a Task and executing it in the background:
Task<MyResult> currentTask;
currentTask = MyTaskFactory.getImportantTask(parameters ...);
currentTask.continueWith(new Continuation<MyResult,MyResult>() {
@Override
public MyResult then(Task<MyResult> task) throws Exception {
MyResult result = task.getResult();
if (!task.isFaulted()) {
if (DEBUG) {
logger.v("got login result back: " + result.toString());
}
if (result.getSuccess()) {
callback.onMySuccess(result.getData());
} else {
callback.onMyError(result.getStatusCode(), result.getError());
}
}
return result;
}
}, Task.UI_THREAD_EXECUTOR);
MyTaskFactory.getImportantTask() returns a Task, and my object under test gets this task and executes it in the background. When it completes, the completion should get executed.
I have verified that in the actual app, this code is working correctly. My problem arises because I am trying to run this under Robolectric/Mockito.
Here is the code under test:
myobject.runTaskWithContinuation(parameters...);
ShadowLog.d(TAG, "Waiting....");
Task<AuthenticationResult> task = controller.getCurrentTask();
assert(task != null);
ShadowApplication.runBackgroundTasks();
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
ShadowLog.d(TAG, "Finished!");
Using this code, and by mocking and instrumenting the calls made by the Task and the Continuation, I have verified that the Task is executing and generating a result, yet the Continuation never gets called. If I wait for the Task to complete, my unit test will never finish.
I am certain that I am doing something wrong here, but I really don't know what. How can I make sure that the Continuation gets called under test?
In your unit tests, since you have access to the Task directly, call
task.waitForCompletion()
For example:
For other tests when you don't have access to the Task reference, you might need to do a little extra work.
This blog post details more about it: Unit Testing Asynchronous Bolts Tasks