I'm trying to set up mock http responses for my te...
# apollo-kotlin
m
I'm trying to set up mock http responses for my tests in a very minimal test, using the
mockServerTest
block in a very minimal test:
Copy code
mockServerTest {
    mockServer.enqueue(mockResponse)
    MyService(apolloClient).getData()
}
However, I just get a
UncompletedCoroutinesError
(test coroutine not completing). The
getData()
function is a suspend function that just calls
apolloClient.query(MyQuery).execute()
, and returns the result.
s
Is it just this test, or all tests using this? How are you setting up your ApolloClient + MockServer?
m
This is my first test using this, so yes to both 😄 I'm using the ApolloClient + MockServer provided through the
mockServerTest
block
This is the code for that setup (in the Apollo code):
Copy code
val mockServer = MockServer()

  val apolloClient = ApolloClient.Builder()
      .serverUrl(mockServer.url())
      .build()
m
Is this native/JS by any chance?
m
Android codebase, JUnit test case
m
Welp, that's unexepcted...
m
How come? 😅
m
Maybe incopatible coroutine versions? Wild guess at this point... Can you share the full stacktrace?
m
My original idea was to try to use the MockServer just to mock a response directly in the Service itself, so I could run the app with mocked response for a specific server, but I couldn't figure out how to get that working either (it didn't like the url of the MockServer). Separate issue though.
stacktrace.txt
s
Are you also using kotlin’s
runTest
somewhere in there? I see that at one place I am using it, I am doing this https://github.com/HedvigInsurance/android/blob/41758c154b9483826864a5f67f0f301caa[…]54/app/app/src/test/kotlin/com/hedvig/app/apollo/ApolloUtils.kt but I don’t remember if I needed that or not. Might be unrelated though, don’t want to steer you too far from your original issue.
Seeing a lot of
runTestCoroutineLegacy
in there, are you using some old coroutine dependency with the old runBlockingTest which is now deprecated?
m
Hmm, the
mockServerTest
block calls kotlin's
runTest
internally, yes
kotlinCoroutines = "1.7.3"
m
Same version Apollo uses internally
m
I copied your
runApolloTest
function, and it definitely seems to work better! Now I'm just getting
Failed to parse GraphQL http network response
, but that's on me 😅 I'll get back to you to verify once this works all the way through. In the meanwhile: Thanks for the help! 🙏
🌟 1
I wonder if it's just the
TestDispatcher
being supplied in your case that makes the difference? 🤔
👀 1
m
mockServer
was mostly intended for internal uses, we should probably hide it or rework the API a bit
Sounds like a dispatcher thing indeed
kotlinx.coroutines
allow you to inject a test dipatcher IIRC?
m
I tried to reduce the code to just this, and it still works the same 🤷
Copy code
fun runApolloTest(
    block: suspend CoroutineScope.(MockServer, ApolloClient) -> Unit,
) {
    return kotlinx.coroutines.test.runTest {
        val mockServer = MockServer()
        val apolloClient = ApolloClient.Builder()
            .serverUrl(mockServer.url())
            .build()
        block(mockServer, apolloClient)
        apolloClient.close()
        mockServer.stop()
    }
}
👀 1
s
Oh, now that’s confusing 😵‍💫 I figured that’s what would’ve made it work, but I was wrong then. So this last snippet works perfectly fine in your case right?
m
I'm still struggling to understand what's wrong with my response body, but I'm getting that error, instead of the other one, yes 😬
s
Maybe time to get even more inspiration from what I’ve done? 😄 We have this https://github.com/HedvigInsurance/android/blob/41758c154b9483826864a5f67f0f301caa[…]54/app/app/src/test/kotlin/com/hedvig/app/apollo/ApolloUtils.kt with sample usage here https://github.com/HedvigInsurance/android/blob/4a89b94a1c26b41b11ac6ecf3995760527[…]est/kotlin/com/hedvig/app/apollo/EmbarkStoryQueryParsingTest.kt. This makes it so that you can take a response object (Operation.Data) just as you would from the backend anyway, and turn it back into Json to feed it into your mock server. You can even combine all this with the test data builders, like here https://github.com/HedvigInsurance/android/blob/4a89b94a1c26b41b11ac6ecf3995760527[…]est/kotlin/com/hedvig/app/apollo/EmbarkStoryQueryParsingTest.kt to make the entire experience very nice tbh.
😄 1
m
Just for the sake of completeness, my graphql parse failed due to not having
__typename
in the body for fragments... fun times! Now everything works, data is parsed, and the test passes! Thanks a lot for the help 🙂
🦠 2
150 Views