0

I have issues running my unit tests with a specific version of Robolectric and Facebook. My test scripts work when I have the following

testCompile "org.robolectric:robolectric:3.3.2"
testCompile "org.robolectric:shadows-multidex:3.3.2"
testCompile 'org.robolectric:shadows-support-v4:3.3.2'

and

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

But, when I upgrade to

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

I get the following issues:

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

Any idea how to fix the issue?

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
Killesk
  • 2,734
  • 3
  • 22
  • 29

1 Answers1

1

Since 4.19, Facebook SDK does an automatic init process. Robolectric doesn't start it properly, hence the crash.

If you don't need Facebook SDK initialized, a perfectly valid solution is shadowing activateApp call. Create this class:

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

and attach it to your test:

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

Ref: http://robolectric.org/extending/