adgllorente
adgllorente

Reputation: 51

NullPointerExcepcion with Robolectric on Activity.onCreate()

I'm using Robolectric to test an Activity (FooActivity) that extends BaseActivity and BaseActivity is extending Activity, but I'm getting a NullPointerException in Activity.onCreate()

FooActivity.java extends BaseActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
}

BaseActivity.java extends Activity

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    this.mActivityCreated = true;
    mFragmentMgr = getFragmentManager();
    this.setContentView(R.layout.main);
}

Test: FooActivityTest

@Before
public void setUp() {
    mActivity = new FooActivity();
}


@Test
public void test() {

    Intent i = new Intent();
    i.putExtra("isTesting", true);
    mActivity.setIntent(i);
    mActivity.onCreate(null);
}

Error

java.lang.NullPointerException
at android.app.Activity.onCreate(Activity.java:874)
at com.pdi.enjoy.activities.BaseActivity.onCreate(BaseActivity.java:129)
at com.pdi.enjoy.activities.FooActivityTest.onCreate(FooActivity.java:107)
at com.pdi.enjoy.activities.FooActivityTest.test(FooActivity.java:32)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)

Upvotes: 1

Views: 2268

Answers (2)

Swaroop Joshi
Swaroop Joshi

Reputation: 53

Looks like an earlier version of Robolectric required you to call the lifecycle methods explicitly, but the later ones are able to handle the lifecycle calls themselves.

Also, one had to use activity.getApplication() to get the application, but it looks like Robolectric.application is directly available now.

@dymmeh: Thanks for you answer.

Upvotes: 1

dymmeh
dymmeh

Reputation: 22306

Intent i= new Intent(Robolectric.application, FooActivity.class);
i.putExtra("isTesting", true);

FooActivity activity = Robolectric.buildActivity(FooActivity.class).withIntent(i).create().get();

Don't manually call onCreate on the activity. Robolectric will take care of that..

From here you can do whatever other tests using the activity object you need.

Upvotes: 6

Related Questions