Mocking GraphQL responses
The test network transports are currently experimental in Apollo Kotlin. If you have feedback on them, please let us know via GitHub issues or in the Kotlin Slack community.
QueueTestNetworkTransport
is a high-level test API that lets you specify the GraphQL responses that are returned by your ApolloClient
instance.
See also MockServer
, which creates a full HTTP server and helps test specific server behaviors, such as error cases, HTTP headers, and timeouts.
Add the dependency to your project's build.gradle
file:
dependencies {testImplementation("com.apollographql.apollo3:apollo-testing-support:4.0.0-beta.7")}
Enable the QueueTestNetworkTransport
by passing it to the ApolloClient
builder:
val apolloClient = ApolloClient.Builder().networkTransport(QueueTestNetworkTransport()).build()
You can then use the enqueueTestResponse
extension function to specify the GraphQL responses to return:
val testQuery = GetHeroQuery("001")val testData = GetHeroQuery.Data {hero = droidHero {name = "R2D2"}}apolloClient.enqueueTestResponse(testQuery, testData)val actual = apolloClient.query(testQuery).execute().data!!assertEquals(testData.hero.name, actual.hero.name)
You can pass an ApolloResponse
to the enqueueTestResponse
function, or as a shortcut, you can pass a Data
directly as shown above. If you do pass a Data
, you also need to pass an operation, because an ApolloResponse
is built under the hood, which needs a reference to it.
To help create your mocked response data, you can use data builders.
Advanced usage
QueueTestNetworkTransport
returns responses in the order they've been enqueued.
In certain tests, it might be more convenient to map responses to operations.
To achieve this, use MapTestNetworkTransport
instead and call the registerTestResponse
extension function:
val apolloClient = ApolloClient.Builder().networkTransport(MapTestNetworkTransport()).build()apolloClient.registerTestResponse(query1, testData1)apolloClient.registerTestResponse(query2, testData2)val actual1 = apolloClient.query(query1).execute().dataval actual2 = apolloClient.query(query2).execute().data// Execute query1 again, which is mapped to testData1val actual3 = apolloClient.query(query1).execute().dataassertEquals(testData1, actual1)assertEquals(testData2, actual2)assertEquals(testData1, actual3)
If you need more control over the responses to return, you can implement your own NetworkTransport
and pass it to the ApolloClient
builder:
private class CustomTestNetworkTransport : NetworkTransport {override fun <D : Operation.Data> execute(request: ApolloRequest<D>): Flow<ApolloResponse<D>> {return flowOf(ApolloResponse.Builder(/* Your custom logic here */).build() as ApolloResponse<D>)}override fun dispose() {}}apolloClient = ApolloClient.Builder().networkTransport(CustomTestNetworkTransport()).build()
Note that if you use a custom NetworkTransport
, calling enqueueTestResponse()
or registerTestResponse()
is no longer possible because they expect the transport to be QueueTestNetworkTransport
or MapTestNetworkTransport
.