gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:36:42 25 4
gpt4 key购买 nike

我正在使用 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)

最佳答案

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

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

不要在 Activity 上手动调用 onCreate。 Robolectric 将负责这件事..

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

关于java - 在 Activity.onCreate() 上使用 Robolectric 的 NullPointerExcepcion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19430855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com