在Activity.onCreate()上使用Robolectric的NullPointerExcepcion

时间:2013-10-17 15:25:19

标签: java android nullpointerexception junit4 robolectric

我正在使用Robolectric来测试扩展BaseActivity的Activity(FooActivity),而BaseActivity正在扩展Activity,但我在Activity.onCreate()中得到了NullPointerException

FooActivity.java 扩展 BaseActivity.java

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

BaseActivity.java 扩展活动

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    this.mActivityCreated = true;
    mFragmentMgr = getFragmentManager();
    this.setContentView(R.layout.main);
}

测试:FooActivityTest

@Before
public void setUp() {
    mActivity = new FooActivity();
}


@Test
public void test() {

    Intent i = new Intent();
    i.putExtra("isTesting", true);
    mActivity.setIntent(i);
    mActivity.onCreate(null);
}

错误

java.lang.NullPointerException
at android.app.Activity.onCreate(Activity.java:874)
at com.pdi.enjoy.activities.BaseActivity.onCreate(BaseActivity.java:129)
at com.pdi.enjoy.activities.FooActivityTest.onCreate(FooActivity.java:107)
at com.pdi.enjoy.activities.FooActivityTest.test(FooActivity.java:32)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 个答案:

答案 0 :(得分:6)

Intent i= new Intent(Robolectric.application, FooActivity.class);
i.putExtra("isTesting", true);

FooActivity activity = Robolectric.buildActivity(FooActivity.class).withIntent(i).create().get();

不要在活动上手动调用onCreate。 Robolectric将负责......

从这里,您可以使用您需要的活动对象进行任何其他测试。

答案 1 :(得分:1)

看起来Robolectric的早期版本要求您明确调用生命周期方法,但后者能够自己处理生命周期调用。

此外,必须使用activity.getApplication()来获取应用程序,但现在可以直接使用Robolectric.application

@dymmeh:谢谢你的回答。