Android Unit Tests with AppCompat project library

不羁岁月 提交于 2019-12-06 14:03:20

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!