1

我正在使用 Robolectric 测试扩展 BaseActivity 的 Activity (FooActivity),而 BaseActivity 正在扩展 Activity,但在 Activity.onCreate() 中出现 NullPointerException

FooActivity.java扩展BaseActivity.java

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

BaseActivity.java扩展了Activity

@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)
4

2 回答 2

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 会解决这个问题的。

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

于 2013-10-17T15:29:55.950 回答
1

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

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

@dymmeh:谢谢你的回答。

于 2014-02-14T19:44:17.533 回答