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

android, android-espresso, integration-testing, retrofit

Solution

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+.

Problem

I am writing integration tests that perform actions in the UI which start network calls using Retrofit. I know I need to implement a `CountingIdlingResource`, 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.

Original source