Question

I recently started to work on the Robolectric unit testing framework for my android apps. I am using Android Studio IDE for this all.

I have made MainActivity.java where there is button R.id.clickButton, I am able to run that app in my phone/emulator and working fine.

I have made MainActivityTest.java where I am trying to see is button not null like below

private MainActivity activity;

@Before
public void setup() {
    activity = Robolectric.buildActivity(MainActivity.class).get();
}

@Test
public void shouldFail() {
    assertTrue(true);
}

@Test
public void shouldNotBeNull() {
    assertThat(activity).isNotNull();

    Button button = (Button) activity.findViewById(R.id.clickButton);
    assertThat(button).isNotNull();

}

When I execute the test using command line then build failed at Button button = (Button) activity.findViewById(R.id.clickButton); with an error trace. I could not find why it so. Guys look this and hoping you will reply me soon.

StackTrace

java.lang.NullPointerException
    at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
    at android.view.ViewGroup.initViewGroup(ViewGroup.java:447)
    at android.view.ViewGroup.__constructor__(ViewGroup.java:417)
    at android.view.ViewGroup.<init>(ViewGroup.java:416)
    at android.widget.FrameLayout.<init>(FrameLayout.java:93)
    at org.robolectric.tester.android.view.RoboWindow$2.<init>(RoboWindow.java:231)
    at org.robolectric.tester.android.view.RoboWindow.createDecorView(RoboWindow.java:231)
    at org.robolectric.tester.android.view.RoboWindow.access$000(RoboWindow.java:30)
    at org.robolectric.tester.android.view.RoboWindow$1.run(RoboWindow.java:222)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
    at org.robolectric.tester.android.view.RoboWindow.getDecorView(RoboWindow.java:220)
    at org.robolectric.tester.android.view.RoboWindow.findViewById(RoboWindow.java:292)
    at org.robolectric.shadows.ShadowActivity.findViewById(ShadowActivity.java:312)
    at android.app.Activity.findViewById(Activity.java)
    at com.example.robolectrichelloworld.MainActivityTest.shouldNotBeNull(MainActivityTest.java:41)
    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:241)
    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:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Was it helpful?

Solution 2

I solved the problem by simply extend the class by Activity. My activity was extending ActionBarActivity that was creating the issue.

OTHER TIPS

Try to insert the call to create() after Robolectric.buildActivity(...):

@Before
public void setup()  {
    activity = Robolectric.buildActivity(MainActivity.class).create().get();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top