I am migrating an app that uses Retrofit
to work with coroutines. The app has some UATs that are failing because Espresso does not wait for the coroutines to complete and asserts immediately.
The CoroutineCallAdapterFactory
, by default, uses OkHttp's Dispatcher
to perform asynchronous requests, but Espresso
only monitors the UI Thread
and AsyncTaks
's Thread
pool. One solution I thought of is to force OkHttp
's Dispatcher to use AsyncTask
's ThreadPoolExecutor
.
val dispatcher = Dispatcher(AsyncTask.THREAD_POOL_EXECUTOR as ExecutorService)
okHttpClientBuilder.dispatcher(dispatcher)
This seems to work and the tests pass.
Is this a bad idea? Is registering an IdlingResource
still a better option?