getSupportActionBar() returns null with Robolectric

こ雲淡風輕ζ 提交于 2019-12-04 05:06:18

Support ActionBar
I was able to get back an instance of the Support ActionBar by adding a @Config annotation to my test with the Gingerbread sdk build number:

@Test @Config(reportSdk = 10)
public void actionbarTest(){
.... Your Test here
}

A simple project setup can be seen here: simple-robolectric



ActionBarSherlock
You have to add modified ActionBarSherlock files to your test package and call the following methods in your @Before method:

ActionBarSherlock.registerImplementation(ActionBarSherlockRobolectric.class);
ActionBarSherlock.unregisterImplementation(ActionBarSherlockNative.class);
ActionBarSherlock.unregisterImplementation(ActionBarSherlockCompat.class);

The full set of instructions can be found here: ActionBar and Robolectric working together

Update
With Robolectric 2.2 you only have to add the config annotation "@Config(reportSdk = 10)" to your test methods or class and it should work as well.

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