Android单元测试与AppCompat项目库



我有一个项目,有一些单元测试扩展了ActivityUnitTestCase,但是当我安装android-support-v7-appcomapt项目库来使用ActionBar时,这些测试已经停止工作了。我还有一些ActivityInstrumentationTestCase2测试,它们工作得很好。这是我的设置

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);
}

如果我不调用startActivity, veActivity是空的。

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

我尝试了在StackOverflow中找到的许多建议,其中大多数都导致了这个例外。

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

使用launchActivity创建了一个解决方案,并确保已启动的活动已经完成。手动设置主题,启动等待空闲同步的活动,并在测试后完成该活动。

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();

我认为你的错误是:

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

必须使用:

setActivityIntent(intent);

你不需要指定何时开始你的活动,它会自动完成

相关内容

  • 没有找到相关文章

最新更新