如何覆盖Robolectric应用程序?

时间:2016-03-07 19:34:04

标签: java android junit robolectric

我继承了传统的Android项目并开始添加Robolectric 3和JUnit 4测试用例。遗憾的是,目前主要的应用程序类无法初始化,因为意大利面条代码和框架在Robolectric中不起作用。我想替换我的应用程序类进行测试。但在我甚至可以更新RuntimeEnviroment.application变量之前,Robolectric崩溃了。 有没有办法在Robolectric中禁用清单应用程序创建?

这是我一直在尝试做的事情:

@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
    }
}

但由于Parse框架而失败:

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 个答案:

答案 0 :(得分:9)

因此,Robolectric开发人员忽略了这种情况,@Config指令允许应用程序覆盖。显而易见的覆盖根本没有帮助我,所以可以删除它。

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

由于我们的应用程序使用单例模式,我还必须初始化单例实例。我忽略了对purpouse的所有其他初始化,因为它主要是在框架中。这就是我的MyTestApplication的样子:

public class MyTestApplication extends MyApplication {
    @Override
    public void onCreate() {
        MyApplication.instance = this;
    }
}

答案 1 :(得分:1)

有一个更简单的解决方案。

只需在测试源中放置一个类Test<YouApplicationName>,然后** Robolectric就会接收它。

因此,如果您的应用程序名称为org.my.package.SuperApplication,则在文件夹TestSuperapplication中创建类test\java\org\my\package\,并且您不需要在@Config部分中提供任何内容。< / p>