Start Activity for testing

t0s picture t0s · May 12, 2015 · Viewed 29.8k times · Source

I 've got a Quiz app using Realm db. Every time the user selects an answer she clicks a button and new text for Question appears. Thats it until she reaches the end where I start a new Activity and display a score based on correct answers.

How should I start/test ( with Espresso I guess ) that activity without having to enter manually every time all the answers and click the button after each answer until I reach the last one?

What I need is to pass some mock data to a variable and make an Intent but I dont know how and cant find anything related with this in Espresso

Answer

chiuki picture chiuki · May 12, 2015

You can launch your next activity with a custom intent like this:

@RunWith(AndroidJUnit4.class)
public class NextActivityTest {

  @Rule
  public ActivityTestRule<NextActivity> activityRule 
     = new ActivityTestRule<>(
        NextActivity.class,
        true,     // initialTouchMode
        false);   // launchActivity. False to customize the intent

  @Test
  public void intent() {
    Intent intent = new Intent();
    intent.putExtra("your_key", "your_value");

    activityRule.launchActivity(intent);

    // Continue with your test
  }
}

Full example: https://github.com/chiuki/android-test-demo

Blog post: http://blog.sqisland.com/2015/04/espresso-21-activitytestrule.html