Does creating an activity using the .withIntent()
not work in Robolectric 2? I'm doing the following
activity = Robolectric.buildActivity(MyActivity.class)
.create()
.withIntent(intent)
.get();
And i'm getting a NullPointerException
when doing the following in the onCreate()
of my activity.
Bundle bundle = getIntent().getExtras();
I can code a null check in my onCreate()
and set the intent by doing the following but it seems redundant to set the intent and call the onCreate()
method again when Robolectric already does that when creating the Activity
instance. This seems like an unnecessary work around.
Robolectric.shadowOf(activity).setIntent(intent);
activity.onCreate(null);
This is a case where a fluent-style API kinds of leads you down the wrong path...
You want to:
activity = Robolectric.buildActivity(MyActivity.class)
.withIntent(intent)
.create()
.get();
so that the intent is provided to the builder before it calls onCreate().
For newer versions of Robolectric use Robolectric.buildActivity(Class, Intent)
.
I figured out my problem. I wasn't instantiating the Intent
properly. I was instantiating it with the no-arg constructor when i needed to give a Context
and the class of the Activity
it was being sent to
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With