Search code examples
androidunit-testingandroid-appcompatactivityunittestcase

Android Unit Tests with AppCompat project library


I have a project with some unit tests that extends ActivityUnitTestCase, but when I installed the android-support-v7-appcomapt project library to use the ActionBar, these tests have stopped working. I also have some ActivityInstrumentationTestCase2 tests and they are working fine. This is my setup

protected void setUp() throws Exception {
    super.setUp();      

    Intent intent = new Intent(getInstrumentation().getTargetContext(), MyActivity.class);              

    startActivity(intent, null, null); // The exception happens here
    veActivity = getActivity();

    //getInstrumentation().callActivityOnStart(veActivity);
}

If I don't call startActivity, veActivity is null.

java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage/mypackage.MyActivity}
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:282)
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:116)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at sysnetlab.android.sdc.ui.ViewExperimentActivity.onCreate(ViewExperimentActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at sysnetlab.android.sdc.test.myActivityTests.setUp(MyActivityTests.java:34)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage/mypackage.MyActivity}
at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:242)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:298)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:279)
... 17 more

I tried many suggestions found here in StackOverflow and most of them lead me to this exception.

android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage/mypackage.MyActivity}

Solution

  • Created a workaround using launchActivity and making sure activities launched are finished. Set the theme manually, launch the activity waiting for idle Sync, and after your tests, finish the activity.

    Context context = getInstrumentation().getTargetContext();
    context.setTheme(R.style.Theme_AppCompat);
    mActivity = launchActivity(context.getPackageName(),
        MyActivity.class, null);
    getInstrumentation().waitForIdleSync();
    
    // YOUR TESTS
    // assertNotNull("The activity cannot be null.", mActivity);
    
    sendKeys(KeyEvent.KEYCODE_BACK);
    mActivity.finish();