Robolectric:actionBar.hide()返回nullactionBar、Robolectric、null、hide

2023-09-12 03:19:17 作者:Roc,|▍

我是新来的Andr​​oid单元测试和我使用Robolectric作为一个测试框架。我用Robolectric 2.2。

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)

该行指,     abs.hide();

This line refers to abs.hide();

注:我试过西安的主旨并没有奏效。

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

我也尝试创建ShadowSherlockActivity如这但我不知道如何使用这个阴影类创建类似的活动:

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

注2:我尝试使用Robolectric快照2.3,但它并没有解决。

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

感谢。

推荐答案

随着@ersentekin和我计算出的评论,这里是一个的要点 < A HREF =htt​​ps://gist.github.com/marsucsb/6059760相对=nofollow> marsucsb ,并修改它与Robolectric 2.2工作+

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+