in Android, Testing

Test your UI on Android with Espresso – Damn you, ProgressBar!

When a user opens an app and the initial data gets fetched from the server, a good pattern is to display a ProgressBar.

When I wanted to test the presence of this loading screen, Espresso was causing me some trouble.

This is the problematic test – looks simple:

public void testShowLoadingScreen(){

The error message is quite revealing:

java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.example.espresso/.activities.LoadingActivity (has extras) } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1431198543157 and now the last time the queue went idle was: 1431198543157. If these numbers are the same your activity might be hogging the event queue.

After fiddling around a while I got my tests running.

Replace all your ProgressBar views with plain views for testing. The solutions are the so called Build Types. I first heard about them when using a different Google Maps API key for release and debug. But there is also a build type called androidTest.

The solution is to copy the problematic layout files to app/src/androidTest/res/layout and replace all ProgressBar views with normal views.

Kinda dirty, but it works.


  1. I just tried overriding the layout files in the Test layout folder. However when running the tests, the “normal” layouts are used still.

    Did you do any changes to the gradle configuration or other setup?

  2. I have tried to create a layout folder under androidTest but when I try to define the namespace xmlns:android=”” on the root element of my layout I get the error: URI is not registered ( Setting | Project Settings | Schemas and DTDs )

    Any ideas?