androidgradleandroid-studiorobolectricbugsense

Robolectric NullPointerException with BugSense


I'm attempting to create a simple Robolectric unit test (using Gradle following to some extent the example here http://www.peterfriese.de/android-testing-with-robolectric/).

Everything seems to be set up correctly, however I'm getting the following whenever I run the tests due to some initialization that occurs in my MyApplication.java class. I feel like it's a long shot, but does anyone know why this might be happening?

java.lang.RuntimeException: java.lang.NullPointerException
        at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:231)
        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:80)
        at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:47)
        at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
        at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        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:66)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
        at java.lang.Thread.run(Thread.java:680)
    Caused by: java.lang.NullPointerException
        at com.bugsense.trace.Utils.CheckNetworkConnection(Unknown Source)
        at com.bugsense.trace.Utils.isWifiOn(Unknown Source)
        at com.bugsense.trace.Utils.setProperties(Unknown Source)
        at com.bugsense.trace.BugSenseHandler.initAndStartSession(Unknown Source)
        at com.app.application.onCreate(MyApplication.java:75)
        at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:146)
        at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:387)
        at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:227)
        ... 35 more

If I comment out the BugSense initialization, I end up with tests succeeding as expected. I'd prefer not to, but if there isn't another solution, is there any recommended way I can check to see if the app is being started by Robolectric?

@Override
public void onCreate() {
    super.onCreate();
    ...
    // Commenting this line out corrects the issue.
    // BugSenseHandler.initAndStartSession(this, getString(R.string.bugsense_key));
    ...
}

Solution

  • I ended up needing to subclass my ProjectApplication class with a TestProjectApplication class which did NOT have the BugSense initialization. I believe this class is automatically used/injected by Robolectric. I can't remember where I found this... will post back if I remember.