Austyn Mahoney Austyn Mahoney - 4 months ago 44
Android Question

How to test Android UI using IdlingResource when using Retrofit network requests

I am writing integration tests that perform actions in the UI which start network calls using Retrofit.

I know I need to implement a

, but I want to do it the correct way (and not reinvent the wheel if it has already been done).

Has anyone implemented an
IdlingResource
in their app's Espresso test suite to wait while network requests execute?

More info here.

Answer

The most straightforward solution for this: is to basically swap out Retrofit's Thread-pool executor with an AsyncTask one (as recommended by the very helpful Nick from that linked Google group discussion). I do this like so:

new RestAdapter.Builder()
               .setEndpoint(LOCLSET_SERVER_URL)
               .setExecutors(AsyncTask.THREAD_POOL_EXECUTOR,
                             new MainThreadExecutor())
               .build();

I'm not sure if this is the most appropriate solution, but it's the quickest most sane one that I could get working. Bare in mind the caveat, that this works only for ICS+.

Comments