Robolectric:测试一个Activity,期待额外的内容(版本2.X)

Jig*_*wda 16 robolectric

我想测试一个活动,其中一些信息在附加内容中传递.

Intent intent = new Intent().putExtra("someData", "asdfgh");
activity = Robolectric.buildActivity(XYZ.class).withIntent(intent).create().get();
Run Code Online (Sandbox Code Playgroud)

它抛出以下错误:

java.lang.NullPointerException
at android.app.Activity.attach(Activity.java:4993)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController.attach(ActivityController.java:92)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:117)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
at org.robolectric.util.ActivityController.create(ActivityController.java:114)
at org.robolectric.util.ActivityController.create(ActivityController.java:126)
at com.XXX.XXX.XYZTest.shouldDoBlah(XYZTest.java:XX)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:241)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
Run Code Online (Sandbox Code Playgroud)

我的问题在某种程度上类似于.但是,答案似乎已经过时了.它在旧版Robolectric中运行良好.

Jig*_*wda 23

这个答案似乎对我有用.但是,有人可以确认这是正确的做法.因为在以前的版本中new Intent().putExtra(String, String)使用起来很好.

这就是我做的:

Intent intent = new Intent(Robolectric.getShadowApplication().getApplicationContext(), XYZ.class);
intent.putExtra("foo", "bar");
XYZ xyz = Robolectric.buildActivity(XYZ.class).withIntent(intent).create().get();
Run Code Online (Sandbox Code Playgroud)

您需要将上下文和类(您要测试)作为Intent构造函数的参数传递.希望能帮助到你.

对于Roboelectric版本3.1+

withIntent(intent) 不推荐使用方法,因为文档意图应该通过构造函数传递.

Robolectric.buildActivity(XYZ.class, intent).create().get();
Run Code Online (Sandbox Code Playgroud)

  • 设置上下文和类解决了我的NPE问题. (2认同)

小智 9

在robolectric 3.x中,获取应用程序上下文的方式有点不同:

        Intent intent = new Intent(ShadowApplication.getInstance().getApplicationContext(),
                ViewTransactionActivity.class);
        intent.putExtra(foo, bar);
        activity = Robolectric.buildActivity(XYZ.class).withIntent(intent).create().get();
Run Code Online (Sandbox Code Playgroud)