Testing AsyncTaskLoaders with Robolectric
Asked Answered
S

2

13

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?

Sine answered 16/8, 2013 at 19:28 Comment(0)
S
1

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.

Sine answered 31/5, 2014 at 1:34 Comment(2)
do you have any examples to share? I'm looking specifically for the "decoupling scheduler" part. Do you do anything differently in your code when subscribing like injecting a common scheduler ? Or is there nifty method just tells Rxjava to execute everything on a specified thread ?Alterant
I pass in a SchedulerFactory to the classes that need it and in production I pass back the asynchronous schedulers (io, newThread, etc) for their respective calls in the factory methods, while in unit tests all calls return the current thread for all of the calls. You can pass a scheduler to an observable.Sine
E
0

I just posted a way i got it to work in this stack ticket: Testing a CursorLoader with Robolectric & Mockito

It involves writing a couple extra shadow classes. I can paste the fix here if requested but I wasn't sure what the convention was for a duplicate answer.

Ericaericaceous answered 18/2, 2014 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.