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
:
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)
1// Register the idlingResource before running your tests (once per client).
2IdlingResource 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}
1public final class TestRunner extends AndroidJUnitRunner {
2 @Override
3 public void onStart() {
4 IdlingResource idlingResource = ApolloIdlingResource.create("ApolloIdlingResource", apolloClient);
5 IdlingRegistry.getInstance().register(idlingResource);
6 // etc...
7
8 super.onStart();
9 }
10}