Robolectric NullPointerException with BugSense



我正在尝试创建一个简单的 Robolectric 单元测试(在某种程度上使用 Gradle 遵循此处的示例 http://www.peterfriese.de/android-testing-with-robolectric/)。

一切似乎都设置正确,但是每当我运行测试时,由于我的 MyApplication.java 类中发生了一些初始化,我都会得到以下内容。我觉得这是一个很长的机会,但有谁知道为什么会发生这种情况?

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

如果我注释掉 BugSense 初始化,我最终会得到测试按预期成功。我不想,但是如果没有其他解决方案,是否有任何推荐的方法可以检查该应用程序是否由Robolectric启动?

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

我最终需要使用没有 BugSense 初始化的 TestProjectApplication 类来子类我的 ProjectApplication 类。我相信这个类是由 Robolectric 自动使用/注入的。我不记得我在哪里找到这个...如果我记得,会回发。

相关内容

  • 没有找到相关文章

最新更新