Espresso, Using Intent Extra

Espresso

As you know for creating a new Espresso test in JUnit 4 style you should use ActivityTestRule to reduce the amount of boilerplate code you need to write. By using it, the testing framework launches the activity under test before each test method annotated with @Test and @before.

@RunWith(AndroidJUnit4.class)
public class UserProfileTest {

  @Rule
  public ActivityTestRule<UserProfileActivity> mActivityTestRule = new ActivityTestRule<>(UserProfileActivity.class);

  @Test
  public void signUpActivityTest() {
    ...
  }
}

So what if you want to send Intent to an activity? Here is the example:

@RunWith(AndroidJUnit4.class)
public class UserProfileTest {

  @Rule
  public ActivityTestRule<UserProfileActivity> mActivityTestRule = new ActivityTestRule<>(UserProfileActivity.class) {
    @Override
    protected Intent getActivityIntent() {
      Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
      Intent intent = new Intent(context, UserProfileActivity.class);
      intent.putExtra("ID", "120");
      return intent;
    }
  };

  @Test
  public void signUpActivityTest() {

  }
}