5

I've inherited legacy Android project and started to add Robolectric 3 and JUnit 4 test cases. Sadly currently the main Application class can't be initialized because of spaghetti code and frameworks that don't work in Robolectric. I'd like to replace my application class for the tests. But Robolectric crashes before I can even update the RuntimeEnviroment.application variable. Is there a way to disable manifest application creation in Robolectric?

Here's what I've been trying to do:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, manifest = Config.NONE)
public class TestClass {
    @Before
    public void setUp() {
        RuntimeEnvironment.application = new Application();
    }

    @Test
    public void testFunc() {
        // some testing goes here
    }
}

But it fails due to Parse framework:

java.lang.RuntimeException
    at org.robolectric.util.SimpleFuture.run(SimpleFuture.java:61)
    at org.robolectric.shadows.ShadowAsyncTask$2.run(ShadowAsyncTask.java:96)
    at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:230)
    at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:85)
    at org.robolectric.util.Scheduler.post(Scheduler.java:72)
    at org.robolectric.shadows.ShadowAsyncTask.execute(ShadowAsyncTask.java:93)
    at android.os.AsyncTask.execute(AsyncTask.java)
    at com.facebook.internal.Utility.loadAppSettingsAsync(Utility.java:771)
    at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:167)
    at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:146)
    at com.parse.FacebookController$FacebookSdkDelegateImpl.initialize(FacebookController.java:187)
    at com.parse.FacebookController.initialize(FacebookController.java:70)
    at com.parse.ParseFacebookUtils.initialize(ParseFacebookUtils.java:108)
    at com.parse.ParseFacebookUtils.initialize(ParseFacebookUtils.java:92)
    at MyApplication.initParse(StrongliftsApplication.java:174)
    at MyApplication.onCreate(StrongliftsApplication.java:112)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    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.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    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.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

2 Answers 2

11

So it happens that Robolectric developers forsaw this situation and @Config directive allows application override. Also manifest override didn't help me at all so it could be removed.

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, application = MyTestApplication.class)
public class TestClass {
    @Test
    public void testFunc() {
        // some testing goes here
    }
}

Since our application uses singleton pattern I had to also init singleton instance. I ignore all other initalization on purpouse since it was mostly initting frameworks. Here's what my MyTestApplication looks like:

public class MyTestApplication extends MyApplication {
    @Override
    public void onCreate() {
        MyApplication.instance = this;
    }
}
1
  • 1
    Can you please share the MyTestApplication class too ? atleast a basic version of your MyTestApplication.
    – LostPuppy
    Apr 4, 2016 at 6:23
2

There is a simpler solution.

Just put a class Test<YouApplicationName> in test sources and **Robolectric will pick up it.

So if your application name is org.my.package.SuperApplication then create class TestSuperapplication in folder test\java\org\my\package\ and you don't need to provide anything in the @Config section.

2
  • Nice to know, didn't find it in the documentation, thanks! Although I prefer to explictly state my mock application, that way the next developer will know what's happening. Also this allows different applications clases for different tests.
    – Ritave
    Mar 9, 2016 at 23:44
  • Sure. It is your code. I don't think you ever will use second application Mar 10, 2016 at 5:59

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.