title: Android JUnit test method hang if more than one test methods exist in the test class
body: <p>I am writing an Android <code>JUnit</code> test for one application. I extend <code>ActivityInstrumentationTestCase2</code> and have more than one test methods in my test class. I do not use Robotium - just a simple Android JUnit test.
  When I run the test methods in one go, the first one passes, the next one hangs. If I run them separately, all pass. What happens in my tests is that in each test method I start the <code>Activity_Under_Test</code> with <code>getActivity()</code>, then do something, which starts another activity and the one under test is automatically destroyed. I try to destroy the newly started activity as well (it was captured by monitor), but it stays open on my device and the next test method hangs on <code>getActivity()</code>. Can you please tell me how I can make the next test methods start the <code>Activity_Under_Test</code> properly? I cannot give actual code, but here is some sample of what I am doing:</p>
  
  <pre><code>public class Test extends ActivityInstrumentationTestCase2&lt; Activity_Under_Test&gt; {
  
      ... // local vars
  
      public Test() {
          super(Activity_Under_Test.class);
      }
  
      @Override
      protected void setUp() throws Exception {
          super.setUp();
  
          instrumentation = getInstrumentation();
          context = instrumentation.getTargetContext();
          prefs = context.getSharedPreferences(...);
      }
  
      @Override
      protected void tearDown() throws Exception {
          if (activity != null) {
              activity.finish();
          }
  
          super.tearDown();
      }
  
      public void test1() {
          ... // change shared preferences so that Activity_ABC is started automatically from Activity_Under_Test
  
          monitor1 = instrumentation.addMonitor(Activity_ABC.class.getName(), null, false);
  
          activity = getActivity();
  
          // Activity_Under_Test starts automatically Activity_ABC and destroys itself
          activityABC = (Activity_ABC) mInstrumentation.waitForMonitorWithTimeout(monitor1, 5);
          assertNotNull(activityABC);
  
          instrumentation.runOnMainSync(new Runnable() {
  
              @Override
              public void run() {
                  instrumentation.callActivityOnDestroy(activityABC);
              }
          });
  
          assertTrue(activityABC.isDestroyed());
          instrumentation.removeMonitor(monitor1);
      }
  
      public void test2() {
          ... // change shared preferences so that Activity_DEF is started automatically from Activity_Under_Test
  
          monitor2 = instrumentation.addMonitor(Activity_DEF.class.getName(), null, false);
  
          activity = getActivity();
  
          // Activity_Under_Test starts automatically Activity_DEF and destroys itself
          activityDEF = (Activity_DEF) mInstrumentation.waitForMonitorWithTimeout(monitor2, 5);
          assertNotNull(activityDEF);
  
          instrumentation.runOnMainSync(new Runnable() {
  
              @Override
              public void run() {
                  instrumentation.callActivityOnDestroy(activityDEF);
              }
          });
  
          assertTrue(activityDEF.isDestroyed());
          instrumentation.removeMonitor(monitor2);
      }
  }
  </code></pre>
  
  <p>If you need more information, please tell me what I shall tell more.</p>
  
  <p>Thank you in advance!</p>