I am trying to write an Instrumentation Test for my Android app.
I\'m running into some weird threading issues and I can\'t seem to find a solution.
My
The accepted answer describes what is going on perfectly.
As an addition, in case someone is curious why Espresso's methods that touch the UI e.g. perform(ViewActions ...)
don't need to do the same, it is simply because they end up doing it later for us.
If you follow perform(ViewActions ...)
you will find it ends up doing the following (in android.support.test.espresso.ViewInteraction
):
private void runSynchronouslyOnUiThread(Runnable action) {
...
mainThreadExecutor.execute(uiTask);
...
}
That mainThreadExecutor
is itself annotated with @MainThread
.
In other words, Espresso also needs to play by the same rules described by David on the accepted answer.