Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing AsyncTaskLoaders with Robolectric

Has anyone had any luck writing unit tests for AsyncTaskLoaders in Robolectric? I tried a modified version of this example for AsyncTask, but it doesn't exhibit the same behavior. In particular onLoadFinished is never called. Are there any workable examples out there for testing Loaders with Robolectric?

like image 459
Christopher Perry Avatar asked Aug 16 '13 19:08

Christopher Perry


1 Answers

Don't use Loaders. They aren't easy to test, and they're tightly coupled to the Fragments and Activities that use them. Instead, use RxJava Observables for your asynchronous data flows. You can de-couple the scheduling and test them running synchronously in your unit tests. It's much cleaner, and much more straightforward.

like image 144
Christopher Perry Avatar answered Oct 07 '22 02:10

Christopher Perry