0

I'm trying to figure out how to test onSavedInstance using the newer AndroidJunit4 and Activity Rules.

@RunWith(AndroidJUnit4.class)
public class MyViewActivityTest{

    @Rule
    public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();

    @Rule
    public ActivityTestRule<MyViewActivity> mActivityRule = new ActivityTestRule<>(MyViewActivity.class);

    @UiThreadTest
    @Test
    public void testOnSavedIntanceState() {
        uiThreadTestRule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent in = new Intent();
                MyViewActivity activity = mActivityRule.launchActivity(in);
                activity.finish();
                activity.recreate();
            }
        });
    }

I get an error not sure if I am barking up the right tree.

java.lang.IllegalStateException: Must be called from main thread at android.app.Activity.recreate(Activity.java:4620)

JPM
  • 9,077
  • 13
  • 78
  • 137

1 Answers1

1

You should be able to run the test with the annotation @UiThreadTest. It works for every test rule that extends UiThreadTestRule. In this case ActivityTestRule happens to do just that.

EDIT:

@UiThreadTest
@Test
public void testOnUIThread() {
    // Test to run on UI thread
}

EDIT:

I just ran it again and remembered that you can't launch the activity on the UI thread. I made this and ran it without complications.

@RunWith(AndroidJUnit4.class)
public class TestActivity {
   @Rule
   public ActivityTestRule<MyViewActivity> activityRule = new ActivityTestRule<>(MyViewActivity.class, true, false);

   @Test
   public void testOnSavedInstanceState() throws Throwable {
       activityRule.launchActivity(new Intent());

       final Activity activity = activityRule.getActivity();

       activityRule.runOnUiThread(new Runnable() {
          @Override
          public void run() {
             activity.finish();
             activity.recreate();
          }
       });
  }
JPM
  • 9,077
  • 13
  • 78
  • 137
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Added UIThreadTest now I get "Empty test suite.". Is there some kind of setup that is need for UIThreadTest, can you show me example using my code above? – JPM Aug 08 '16 at 21:07
  • You need both `@UIThreadTest` and `@Test`. They can just both be above the method. – DeeV Aug 08 '16 at 21:09
  • Still same error...have you tried this code or are you just throwing out suggestions? – JPM Aug 08 '16 at 21:21
  • I haven't tried this exact code, but I have tests that use this combination of annotations, yes. Or are you saying you're getting the original error (IllegalStateException)? – DeeV Aug 08 '16 at 21:25
  • You don't need both rules. Just the one ActivityTestRule. It inherits from `UiThreadTestRule` so you don't need to include it. – DeeV Aug 08 '16 at 21:25
  • Not following you...and nothing I am trying is working. Google does not have any good examples or explanations, think I will go back to Robo or something that is 10 times easier to understand. Maybe you can show me some of your test code for examples I can actually use in reference to my test code. – JPM Aug 08 '16 at 21:29
  • @JPM: I made an edit that showed what I'm trying to say. You can't actually use the annotation if you are launching the activity from within the test itself. – DeeV Aug 08 '16 at 21:39