Robolectric 3.3.2 Facebook 4.3及以上的兼容性问题

时间:2017-05-03 10:25:18

标签: facebook-android-sdk robolectric

我在使用特定版本的Robolectric和Facebook运行我的单元测试时遇到问题。当我有以下

时,我的测试脚本会起作用
testCompile "org.robolectric:robolectric:3.3.2"
testCompile "org.robolectric:shadows-multidex:3.3.2"
testCompile 'org.robolectric:shadows-support-v4:3.3.2'

compile 'com.facebook.android:facebook-android-sdk:4.2.0'

但是,当我升级到

compile 'com.facebook.android:facebook-android-sdk:4.22.0'

我遇到以下问题:

WARNING: unknown service appops
WARNING: no system properties value for persist.radio.multisim.config

The Facebook sdk must be initialized before calling activateApp

    at com.facebook.appevents.AppEventsLogger.activateApp(AppEventsLogger.java:226)
    at com.facebook.appevents.AppEventsLogger.activateApp(AppEventsLogger.java:208)
    at ladenzeile.android.services.tracking.TrackingService.trackActivateApp(TrackingService.java:129)
    at ladenzeile.android.new_app.NewHome.activity.AppCompatAbstractBaseActivity.onResume(AppCompatAbstractBaseActivity.java:328)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
    at android.app.Activity.performResume(Activity.java:6076)
    at org.robolectric.util.ReflectionHelpers$6.run(ReflectionHelpers.java:198)
    at org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:341)
    at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:192)
    at org.robolectric.android.controller.ComponentController$1.run(ComponentController.java:75)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:362)
    at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:40)
    at org.robolectric.android.controller.ComponentController.invokeWhilePaused(ComponentController.java:72)
    at org.robolectric.android.controller.ActivityController.resume(ActivityController.java:171)
    at org.robolectric.android.controller.ActivityController.setup(ActivityController.java:245)
    at org.robolectric.Robolectric.setupActivity(Robolectric.java:97)
    at ladenzeile.android.new_app.newPreference.RegionPreferenceInteractorTest.saveLanguageAndCountryCode(RegionPreferenceInteractorTest.java:38)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:488)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:209)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:63)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)


Process finished with exit code 255

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

自4.19以来,Facebook SDK执行自动初始化过程。 Robolectric没有正确启动它,因此崩溃了。

如果您不需要初始化Facebook SDK,那么完全有效的解决方案就是屏蔽activateApp电话。创建这个类:

@Implements(AppEventsLogger.class)
public class ShadowAppEventsLogger {
    @Implementation
    public static void activateApp(Application application) {
        // Do nothing, just shadow
    }
}

并将其附加到您的测试中:

@Config(shadows = {ShadowAppEventsLogger.class, ...}, ... )
@Test public void fooTest() {...}

参考:http://robolectric.org/extending/