The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.

Testing with Espresso

Basic JUnit4 instrumentation tests are fine for testing non-UI logic. They even work acceptably for some basic UI testing. The more complex your UI testing gets, though, the more likely it is that you will find plain JUnit4 instrumentation tests to be limiting and tedious.

In particular, running tests across activities can be tricky with ordinary JUnit4. ActivityTestRule is designed for testing a single activity in isolation, and crafting your own rule that transcends a single activity may be difficult.

Espresso is designed to simplify otherwise-complex UI testing scenarios, such as:

In this chapter, we will explore how to set up basic Espresso tests and how to employ them as part of your overall testing implementation.


This chapter assumes that you have read the chapter on JUnit4.

Adding a Shot of Espresso

The Testing/Espresso sample project is the home of several test cases that employ Espresso, so we can see how it works in practice.

The app/ module’s build.gradle file is fairly conventional, reminiscent of our JUnit4 equivalent, except that we have several dependencies:

apply plugin: ''

dependencies {
    implementation ""
    androidTestImplementation ''
    androidTestImplementation ''
    androidTestImplementation ''
    androidTestImplementation ''
    implementation ''

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 27
        applicationId ""
        testApplicationId ""
        testInstrumentationRunner ""
        testInstrumentationRunnerArguments disableAnalytics: 'true'

    packagingOptions {
        exclude 'LICENSE.txt'

The sample app has activities for a ListView and a RecyclerView, and so we implementation in the recyclerview-v7 dependency for that reason. However, beyond that, there are four androidTest dependencies, to pull in things needed for instrumentation testing.

The big one is espresso-core, which contains the bulk of the Espresso test engine. Through Gradle’s transitive dependencies, pulling in espresso-core also pulls in other key testing artifacts, such as:

Hence, just by asking for espresso-core, we pull in everything that we need not only for basic Espresso testing but also for general JUnit4-style instrumentation testing.

However, the support-annotations requested by espresso-core is an older version than the one that we are using in our code under test. So, we specifically pull in the newer support-annotations, allowing Gradle’s dependency resolution to handle the version discrepancy.

The espresso-contrib and recyclerview-v7 androidTest dependencies are for testing RecyclerView. Those artifacts will be discussed later in this chapter.

The test cases themselves still reside in the androidTest/ source set and still use @RunWith(AndroidJUnit4.class). Those aspects of instrumentation testing have not changed, just because we are using Espresso. And we are able to write classic Espresso-free instrumentation tests as well — we are not forced to use Espresso for everything, just because Espresso is part of our environment. So, for example, SillyTest in this sample project is the same as before:


import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

public class SillyTest {
  static public void doThisFirstOnlyOnce() {
    // do initialization here, run once for all SillyTest tests

  public void doThisFirst() {
    // do initialization here, run on every test method

  public void doThisLast() {
    // do termination here, run on every test method

  static public void doThisLastOnlyOnce() {
    // do termination here, run once for all SillyTest tests

  public void thisIsReallySilly() {
    Assert.assertEquals("bit got flipped by cosmic rays", 1, 1);

Writing Tests in Espresso

The preview of this section was lost due to a rupture in the space-time continuum.

The Espresso Test Recorder

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

Stronger Espresso

The preview of this section was fed to a gremlin, after midnight.

Opting Out of Analytics

The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.

Waiting for the World to Change

The preview of this section was lost in the sofa cushions.