ActivityTestRule - 如何在应用程序的 onCreate 之前调用代码
ActivityTestRule - how to call code before Application's onCreate
我正在使用 Espresso 2.1 和 ActivityTestRule,我正在寻找一种方法来在我的应用程序中调用 onCreate()
之前设置一些静态标志。
我有一些我不想在仪器测试期间调用的初始化代码。
Application onCreate()
在 Instrumentation onCreate()
之后调用。对于这种情况,您需要实施自定义测试 运行ner,它将子类化 AndroidJUnitRunner 并将使用您的自定义设置覆盖 callApplicationOnCreate()。
public class MyCustomTestRunner extends AndroidJUnitRunner {
@Override
public void callApplicationOnCreate(Application app) {
InstrumentationRegistry.getTargetContext().getSharedPreferences().doMyStuff();
super.callApplicationOnCreate(app);
}
}
确保更新 build.gradle 中的默认配置以使用新的 testInstrumentationRunner,如下所示:
testInstrumentationRunner "com.myapp.MyCustomTestRunner"
如果您正在寻找 运行 Activity onCreate()
之前的一些代码,请使用您自己的实现子类 ActivityTestRule共 beforeActivityLaunched()
我正在使用 Espresso 2.1 和 ActivityTestRule,我正在寻找一种方法来在我的应用程序中调用 onCreate()
之前设置一些静态标志。
我有一些我不想在仪器测试期间调用的初始化代码。
Application onCreate()
在 Instrumentation onCreate()
之后调用。对于这种情况,您需要实施自定义测试 运行ner,它将子类化 AndroidJUnitRunner 并将使用您的自定义设置覆盖 callApplicationOnCreate()。
public class MyCustomTestRunner extends AndroidJUnitRunner {
@Override
public void callApplicationOnCreate(Application app) {
InstrumentationRegistry.getTargetContext().getSharedPreferences().doMyStuff();
super.callApplicationOnCreate(app);
}
}
确保更新 build.gradle 中的默认配置以使用新的 testInstrumentationRunner,如下所示:
testInstrumentationRunner "com.myapp.MyCustomTestRunner"
如果您正在寻找 运行 Activity onCreate()
之前的一些代码,请使用您自己的实现子类 ActivityTestRule共 beforeActivityLaunched()