UI Tests


Apollo Kotlin provides a built-in IdlingResource to help you write UI tests with Espresso. The ApolloIdlingResource makes sure that your tests wait for your GraphQL queries to terminate before proceeding.

Add the apollo-idling-resource dependency:

Kotlin
build.gradle[.kts]
1implementation("com.apollographql.apollo3:apollo-idling-resource:3.8.5")

If you have multiple ApolloClients, you need to create and register a different ApolloIdlingResource with a different name for each. Registering multiple IdlingResources with the same name will cause your test suite to crash.

Kotlin
1// Create your IdlingResource
2val idlingResource = ApolloIdlingResource("apolloIdlingResource")
3
4// Register the idlingResource before running your tests (once per client).
5IdlingRegistry.getInstance().register(idlingResource)
6
7// Intruct your ApolloClient to update the IdlingResource
8val apolloClient = ApolloClient.Builder()
9  .serverUrl("https://example.com/graphql")
10  .idlingResource(idlingResource)
11  .build()

In the example above, all operations executed by apolloClient (except subscriptions, which might run for a long time) will update the IdlingResource so that no automatic actions take place while your app is waiting for data.

Feedback

Edit on GitHub

Forums