UI Tests


Apollo Android offers a built-in IdlingResource to help writing UI tests with Espresso. The ApolloIdlingResource will make sure that your tests wait for your GraphQL queries terminate before moving on with testing.

Add the following dependency:

apollo-idling-resource

Groovy
1implementation("com.apollographql.apollo:apollo-idling-resource:x.y.z")

If you have multiple ApolloClients you need to create and register multiple ApolloIdlingResource with different names. Registering several IdlingResources with the same name will crash.

Kotlin
Java
1// Register the idlingResource before running your tests (once per client).
2val idlingResource = ApolloIdlingResource.create("ApolloIdlingResource", apolloClient)
3IdlingRegistry.getInstance().register(idlingResource)

Most frequently this code is put into a custom TestRunner as below. Please note that you need the ApolloClient instance you use in the app.

Kotlin
Java
1class TestRunner : AndroidJUnitRunner() {
2    override fun onStart() {
3        val idlingResource = ApolloIdlingResource.create ("ApolloIdlingResource", apolloClient);
4        IdlingRegistry.getInstance().register(idlingResource);
5
6        super.onStart();
7    }
8}