ActivityTestRule - 如何调用code应用程序的onCreate前 [英] ActivityTestRule - how to call code before Application's onCreate

查看:931
本文介绍了ActivityTestRule - 如何调用code应用程序的onCreate前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的长者preSSO 2.1 ActivityTestRule,我正在寻找一种方法来设置一些静态的标志前的onCreate()在我的应用程序将被称为。

I am using Espresso 2.1 with ActivityTestRule, and I am looking for a way to set some static flags before onCreate() in my application will be called.

我有一些初始化code,我不希望在仪器测试调用。

I have some init code that I don't want called during instrumentation tests.

推荐答案

ActivityTestRule自带的beforeActivityLaunched方法,你可以重写,它会被称为活动推出的权利之前。

ActivityTestRule comes with the beforeActivityLaunched method which you can override and it will be called right before activity is launched.

编辑:

原来,应用程序的onCreate被称为仪表的onCreate()之后。在这种情况下,您可以创建自定义的测试运行,这将延长AndroidJUnitRunner并与您的自定义设置覆盖callApplicationOnCreate()。

Turns out that Application onCreate is called after Instrumentation onCreate(). In this case you may create your custom test runner which will extend AndroidJUnitRunner and will override the callApplicationOnCreate() with your custom setup.

public class MyCustomTestRunner extends AndroidJUnitRunner {
@Override
public void callApplicationOnCreate(Application app) {
    InstrumentationRegistry.getTargetContext().getSharedPreferences().doMyStuff();
    super.callApplicationOnCreate(app);
}
}

请确保更新您的defaultConfig中的build.gradle使用新testInstrumentationRunner

Make sure to update your defaultConfig in the build.gradle to use new testInstrumentationRunner

这篇关于ActivityTestRule - 如何调用code应用程序的onCreate前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆