Robolectric: testing an Activity, expecting extras (version 2.X)
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();
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();