Idling Registry for OkHttp

A lot apps use Retrofit or OkHttp for the networking, and when it comes to testing the networked operations, then it becomes a little tricky. As network calls can take a little time, and test fails early due to the delay. In such cases, Idling Registry is used for testing.

If you are using Retrofit or OkHttp, then there’s this OkHttp Idling Resource which makes testing idling resources and network calls a lot more easier with few lines of code.

For example, we have a simple login service, which returns success value in case of successful login and displays it on a TextView with txtStatus ID. So, the test might be something like this.

    public void testLogin()

Now, when we run the test, it will fail. Even if there is successful login. The reason for this is that unit test is faster than the network call and it is not waiting for the response from server. And that’s why it fails. So, we need our test to wait for the response or we need our test to be idle until some response is returned from the network call. This is where idling registry and idling resources are useful.

OkHttp Idling Resource

You can read more about OkHttp Idling Resource at here. We will add two dependencies, one will be for Espresso Idling Registry and other will be for OkHttp Idling Resource.

    debugImplementation ''
    debugImplementation 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0'

Now, we need to create idling resource. So let’s create class.

public abstract class IdlingResources
    public static void registerOkHttp(OkHttpClient client)
        IdlingRegistry.getInstance().register(OkHttp3IdlingResource.create("okhttp", client));

Since, you don’t want to put this class in release build, so its a good practice to create same class with empty function body in the release package using android flavors. This dummy class can look like this.

public abstract class IdlingResources
    public static void registerOkHttp(OkHttpClient client)
        // Dummy and Empty Body

Now, when we are setting up our Retrofit instance, we can setup this resource for OkHttp there like this:

    OkHttpClient client = new OkHttpClient();
    IdlingResources.registerOkHttp(client);         // THIS IS THE IMPORTANT LINE
    Retrofit retrofit = new Retrofit.Builder()

Now, when we run the test, they will be passed.

If you liked this article, you can read my new articles below:

Wajahat Karim is Pakistan’s first Google Developer Expert in Android. As an experience Android developer, he deeply cares about it and keeps writing and speaking about it. He has written two worldwide 300+ pages books on Android development with more than 100 articles around the internet either on his website or his Medium publications. He is also a passionate contributor in open source and has created many Android libraries used by thousands of developers in their apps worldwide. As active public speaker, he spends lots of time giving talks in conferences and motivating people on Android development. You can find Wajahat most active on Twitter @WajahatKarim where he regularly shares all the good stuff about Android and community building.

Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan