title: Android - Espresso test with pressBack
body: <p>I am trying to test my application with the Espresso framework. It should be tested whether the application is exited when pressing back in the main activity and whether the main application is displayed when calling another activity from the main activity and then pressing back.</p>
  
  <pre><code>public class MainActivityTest {
      @Rule
      public IntentsTestRule&lt;MainActivity&gt; intentsTestRule = new IntentsTestRule&lt;&gt;(
              MainActivity.class
      );
  
      @Test
      public void test_pressBack() {
          try {
              pressBack();
              fail();
          } catch (NoActivityResumedException exc) {
              // test successful
          }
      }
  
      @Test
      public void test_anotherActivity_pressBack() {
          onView(withId(R.id.button1)).perform(click());
          pressBack();
          intended(hasComponent(new ComponentName(getTargetContext(), MainActivity.class)));
      }
  }
  </code></pre>
  
  <p>For the first scenario (exit app), I am catching <code>NoActivityResumedException</code>, but this does not seem like the right way to do this.</p>
  
  <p>For the second scenario (return to main activity), I get an intent error:</p>
  
  <pre><code>android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.
  
  IntentMatcher: has component: has component with: class name: is "myPackage.MainActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String
  
  Matched intents:[]
  
  Recorded intents:
  -Intent { cmp=myPackage/.AnotherActivity} handling packages:[[myPackage]])
  </code></pre>