I am writing instrumentation tests using Square's MockWebServer to mock server responses and OkHttpIdlingResource to wait for OkHttp to finish work until the remaining test steps continue. I am also using a MockWebServer dispatcher so that I can respond to certain requests differently depending on the path.
I am running into an issue where the tests work for some Android API versions and they don't work for others.
On Marshmallow the tests pass as expected. When I run on Android Pie, the MockWebServer responds to several requests, but then, for some reason, OkHttp throws a java.net.ConnectException.
If I hit my real web server everything works fine for any app version, it's only when using MockWebServer that I see this issue.
I have no idea what would cause this and the logs are not surfacing anything useful. In the failure case I can see that the MockWebServer has several successful 200 responses, but for some reason a ConnectException occurs during one of the calls.
The only weird thing I can see in the logs is that an Espresso ViewAssertion is logged before the last, failing, OkHttp request is logged. So it may be that the OkHttpIdlingResource is saying that it is idle when it really isn't.
Any ideas on what could be wrong?