Unit test fail with Error(MockKException) for MockResponse()

754 views Asked by At

I have a Client that will make a request to a GraphQL end point as below

import com.apollographql.apollo.ApolloClient

internal class GraphQLClient(apolloClient:ApolloClient, retryStrategy:RetryStrategy){

   override fun <D : Operation.Data, T, V : Operation.Variables> mutate(mutate: Mutation<D, T, V>): Flow<T> {
        val response = apolloClient.mutate(mutate)
        val responseFlow = response.toFlow()
        return responseFlow.map<Response<T>, T> {
            it.data ?: throw ResponseError("Data Null")
        }.retryWhen { cause, attempt ->
            retryStrategy.retry(cause,attempt)
        }
    }
}

I'm testing the above using MockWebServer to create mock responses

  • JUnit test
  • Using Turbine to test Flow
  • I'm trying to validate that for a successful update request the retry logic doesn't get executed
   @Test
    fun `GIVEN a successful update, THEN don't retry`() = runBlocking {
        val server = MockWebServer()
        val mockResponse = MockResponse()
        //Successful response in json format. It's the correct format.
        mockResponse.setBody(readFileFromResources("mock_success_response.json"))
        mockResponse.setResponseCode(200)
        server.enqueue(mockResponse)
        server.start()

        val url = server.url("http://loalhost:8080")
        val apolloClient: ApolloClient = ApolloClient.builder()
            .okHttpClient(OkHttpClient())
            .serverUrl(url.toString())
            .addCustomAdapters()
            .build()

        val retryStrategy = mockk<RetryStrategy>()
        val graphQLClient = GraphQLClient(apolloClient)

        //The mutation of intrest
        val mutation = UpdateMutation(
            SomeInput(
                "123"
            )
        )

        //note how i haven't mocked anything related to retry strategy cause this test doesn't need that

        graphQLClient.mutate(mutation).test {
            verify(exactly = 0) { retryStrategy.retry(any(),any()) }
            expectComplete()
        }

        server.shutdown()

    }

However, my test fails with app.cash.turbine.AssertionError: Expected complete but found Error(MockKException)

Further down the stack trace, I can see the complaint over the lack of answer for some re-try logic related things But i think that's a cause of the above exception being thrown and in reality, shouldn't even be executed Caused by: io.mockk.MockKException: no answer found for: RetryStrategy(#1).isError(com.apollographql.apollo.exception.ApolloNetworkException: Failed to execute http call)

P:S- I'm probably testing a little too much here too but keen to understand what's going on

Things I've tried Just changed the response to an empty string if that has an impact but no change in error. Which makes me think it probably has nothing to do with the response data,

Thanks

1

There are 1 answers

0
HMK On BEST ANSWER

The problem was with this line

 val url = server.url("http://loalhost:8080")

MockServer is not expecting host or port

 val url = server.url("/somepath")