Question

I'm trying to test using robolectric and getting the error below. Seems like other people have received this error when adding an Intent after create() but I'm not doing manipulating the intent at all. Maybe I need to do something special for this because it uses fragments, but that's just a guess. Any help or direction would be appreciated.

StackTrace:

java.lang.NullPointerException
    at android.app.Activity.attach(Activity.java:4967)
    at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
    at org.robolectric.util.ActivityController.attach(ActivityController.java:90)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:114)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
    at org.robolectric.util.ActivityController.create(ActivityController.java:111)
    at org.robolectric.util.ActivityController.create(ActivityController.java:123)
    at com.LocationsResultsMapTest.setUp(LocationsResultsMapTest.java:24)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

LocationsResultsMapTest.java

@RunWith(RobolectricTestRunner.class)
public class LocationsResultsMapTest {

    private MockMapActivity activity;
    private ActivityController<MockMapActivity> activityController;

    @Before
    public void setUp() throws Exception {
        activityController = Robolectric.buildActivity(MockMapActivity.class);
        activity = activityController.create().get();
    }

    @Test
    public void test() {

    }
}

MockMapActivity.java

public class MockMapActivity extends MapActivity {

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
    }
}
Was it helpful?

Solution

I have the same issue.

Here is my current workaround:

Create your custom shadow (for example ShadowMapActivityWorkaround) from the ShadowMapActivity (get it from robolectric sources)

Make the following changes:

public void __constructor__() {
    super.__constructor__();
}

and

@Implementation
public void onResume() {
    registerReceiver(connectivityBroadcastReceiver, new IntentFilter());
    field("mCalled").ofType(boolean.class).in(realActivity).set(true);
}

add the shadow to org.robolectric.Config.properties for example:

shadows: com.mytests.shadows.ShadowMapActivityWorkaround
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top