Jigish Chawda
Jigish Chawda

Reputation: 2178

Robolectric: testing an Activity, expecting extras (version 2.X)

I want to test an activity, to which some information is passed in extras.

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

It throws following error:

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)

My question is similar to this in some manner. However, the answer seems to be outdated. It would have worked well in older versions of Robolectric.

Upvotes: 17

Views: 6003

Answers (4)

Swaroop
Swaroop

Reputation: 572

It seems withIntent method is deprecated. Alternative is:

Intent intent = new Intent();
Intent.putExtra("extra", "test");

Activity activity = Robolectric.buildActivity(YourActivity.class, intent).create().visible().get();

Upvotes: 0

Jigish Chawda
Jigish Chawda

Reputation: 2178

This answer seems to work for me. However, can somebody confirm this as the correct way of doing. Because in the previous versions new Intent().putExtra(String, String) used to work fine.

This is what I did:

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

You need to pass a context and the class (you want to test) as parameters of the Intent constructor. Hope it helps.

For Roboelectric Version 3.1+

withIntent(intent) method is deprecated, as per the document intent should be passed through the constructor instead.

Robolectric.buildActivity(XYZ.class, intent).create().get();

Upvotes: 24

KOTIOS
KOTIOS

Reputation: 11194

In 3.0 snapshot of robolectric the api is changed to :

   Bundle bundle = new Bundle();
   bundle.putString(Constants.PASSWORD_RESET_MESSAGE, "");
   intent.putExtras(bundle);
   mTargetActivity = Robolectric.buildActivity(Target.class).withIntent(intent).create().get();

Upvotes: 1

Javi Martínez
Javi Martínez

Reputation: 512

In robolectric 3.x the way to get the application context is a little different:

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

Upvotes: 9

Related Questions