Mocking HTTP responses (experimental)
MockServer
is still experimental and most of the APIs are designed for Kotlin. MockServer
is multiplatform but if you're using Java only, we recommend using OkHttp MockWebServer which will provide idiomatic Java APIs.
MockServer
implements an HTTP server that you can use to mock responses. It's useful for testing specific server behaviors, such as error cases, HTTP headers, and timeouts. Using it requires minimal changes to your production code, because you only need to change your serverUrl
.
See also TestNetworkTransport
, which handles mocking at the GraphQL level instead of the HTTP level.
Add the dependency to your project's build.gradle
file:
dependencies {testImplementation("com.apollographql.mockserver:apollo-mockserver:0.0.1")}
And here's how to use it:
// Create a mock serverval mockServer = MockServer()// Provide its URL to your ApolloClientval apolloClient = ApolloClient.Builder().serverUrl(mockServer.url()).store(store).build()// Enqueue HTTP responsesmockServer.enqueueString("""{"data": {"random": 42}}""")mockServer.enqueue(MockResponse(body = "Internal server error",statusCode = 500,headers = mapOf("X-Test" to "true"),// Optionally pass a delay to simulate network latencydelayMillis = 1000L,))// Execute queriesval response1 = apolloClient.query(GetRandomQuery()).execute()val response2 = apolloClient.query(GetRandomQuery()).execute()// Don't forget to stop the server when you're donemockServer.stop()
The enqueue
function normally takes a MockResponse
as a parameter, but it also supports a shorthand version that takes a String
(both are shown above).
Advanced usage
By default, MockServer
is configured with a QueueMockServerHandler
, which returns responses in the order they've been enqueued. If you need more control over the responses to return, you can implement your own MockServerHandler
and pass it to the MockServer
:
val customHandler = object : MockServerHandler {override fun handle(request: MockRequest): MockResponse {return if (/* Your custom logic here */) {MockResponse(body = """{"data": {"random": 42}}""",headers = mapOf("X-Test" to "true"),)} else {MockResponse(body = "Internal server error",statusCode = 500,)}}}val mockServer = MockServer(customHandler)
Note that if you use a custom MockServerHandler
, calling MockServer.enqueue()
is no longer possible because it expects the handler to be
a QueueMockServerHandler
.