Robolectric: actionBar.hide() returns Null

懵懂的女人 提交于 2019-12-11 12:38:59

问题


I'm new to Android unit testing and I'm using Robolectric as a testing framework. I use Robolectric 2.2.

I'm trying to test an activity which is like that :

public class LoginActivity extends SherlockActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_login);

ActionBar abs = getSupportActionBar();
abs.hide();
}

and here's my test class :

@RunWith(RobolectricTestRunner.class)
public class LoginActivityTest {

@Before 
public void setUp() throws Exception
{
    System.setProperty("dexmaker.dexcache", "/sdcard");
    activity = Robolectric.buildActivity(LoginActivity.class).create().get();
}

@Test
public void should_loginActivity_created() throws Exception {

    assertNotNull(activity);

}

I'm getting this error :

java.lang.NullPointerException  
at auth.LoginActivity.onCreate(LoginActivity.java:119)

This line refers to abs.hide();

NOTE : I tried Xian's Gist and it did not work.

Also I try to create ShadowSherlockActivity like this But I have no idea how to use this shadow class to create activity like :

activity = Robolectric.buildActivity(LoginActivity.class).create().get();

NOTE 2 : I try to use Robolectric Snapshot 2.3 but it did not solved.

Thanks.


回答1:


As @ersentekin and I worked out in the comments, here is a Gist that takes @Xian's Gist with a suggestion from marsucsb and modifies it to work with Robolectric 2.2+




回答2:


Have u added the android:theme in your androidmanifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:debuggable="false" android:theme="@style/Theme.Sherlock">


来源:https://stackoverflow.com/questions/20262336/robolectric-actionbar-hide-returns-null

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