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.
Robolectric is a framework that allows you to write unit tests and run them on a desktop JVM while still using Android API. Robolectric provides a JVM compliant version of the android. jar file.
Robolectric is intended to be fully compatible with Android's official testing libraries since version 4.0. As such we encourage you to try these new APIs and provide feedback. At some point the Robolectric equivalents will be deprecated and removed.
So the main benefit about Robolectric is that it is a lot faster than Espresso or Instrumented tests in general. The downside is that it fakes an Android environment which you should be aware of. To validate real world problems, better use a classic Android Framework approach.
Robolectric provides a JVM compile version of the android. jar file. Robolectric handles views, resource loading, and many other things that are implemented in the Android native. This enables you to run your Android tests in your development environment, without requiring any other setup to run the test.
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();
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();
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