简体   繁体   English

带有AppCompat项目库的Android单元测试

[英]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. 我有一个项目,该项目带有一些扩展ActivityUnitTestCase的单元测试,但是当我安装了android-support-v7-appcomapt项目库以使用ActionBar时,这些测试已停止工作。 I also have some ActivityInstrumentationTestCase2 tests and they are working fine. 我也有一些ActivityInstrumentationTestCase2测试,它们工作正常。 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. 如果我不致电startActivity,则veActivity为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. 我尝试了在StackOverflow此处找到的许多建议,其中大多数将我引向此异常。

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

Created a workaround using launchActivity and making sure activities launched are finished. 使用launchActivity创建了一种解决方法,并确保启动的活动已完成。 Set the theme manually, launch the activity waiting for idle Sync, and after your tests, finish the activity. 手动设置主题,启动活动以等待空闲的Sync,然后在测试之后完成活动。

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

I think your mistake is : 我认为您的错误是:

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

You must use : 您必须使用:

setActivityIntent(intent);

You don't need to specify than you start your activity, it will be done automatically. 您无需指定即可开始活动,它会自动完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM