Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在androidTest期间调用onCreate()之前访问应用程序_Java_Android_Junit_Android Testing_Android Instrumentation - Fatal编程技术网

Java 在androidTest期间调用onCreate()之前访问应用程序

Java 在androidTest期间调用onCreate()之前访问应用程序,java,android,junit,android-testing,android-instrumentation,Java,Android,Junit,Android Testing,Android Instrumentation,MyApplication类(扩展应用程序)启动时,将启动一项服务以检查SharedReference值。如果该值为false,则会初始化某些数据 出于测试目的,我想将此SharedReference修改为true,这样就不会发生数据初始化。但是,当我这样做时,会出现竞争条件,在我有机会修改首选项之前调用MyApplication.onCreate() 在调用MyApplication.onCreate()之前,如何修改首选项?这样的事情可能吗 MyApplication.java public

MyApplication
类(扩展
应用程序
)启动时,将启动一项服务以检查
SharedReference
值。如果该值为
false
,则会初始化某些数据

出于测试目的,我想将此
SharedReference
修改为
true
,这样就不会发生数据初始化。但是,当我这样做时,会出现竞争条件,在我有机会修改首选项之前调用
MyApplication.onCreate()

在调用
MyApplication.onCreate()
之前,如何修改首选项?这样的事情可能吗

MyApplication.java

public class MyApplication extends Application {
    public static AppComponent component;

    public MyApplication() {
    }

    @Override
    public void onCreate() {
        injectDependencies();

        super.onCreate();
        initializeDatabase();
    }

    private void injectDependencies() {
        if (component == null)
            component = DaggerAppComponent.builder()
                    .appModule(new AppModule(this))
                    .build();

        component.inject(this);
    }

    @VisibleForTesting
    public static void setComponent(AppComponent component) {
        MyApplication.component = component;
    }

    private void initializeDatabase() {
        Intent intent = DataInitializationService.createIntent(this, null);
        startService(intent);
    }
}
SearchFragmentTest.java(src/androidTest风格)

公共类SearchScreenFragmentTest{
@统治
public ActivityTestRule activityRule=新ActivityTestRule(
SearchActivity.class,
true,//initialTouchMode
false);//launchActivity.false设置每个方法的意图
公共搜索ScreenFragmentTest(){
}
@以前
public void setUp()引发异常{
//在“MyApplication.onCreate()”之后运行。我希望在此之前调用它。
新的TestUtils.TestDataInitter(InstrumentationRegistry.getTargetContext())
.clearPrefs(真)
.acceptIntro(正确)
.initDatabaseFake(真)
.init();
}
@之后
public void tearDown()引发异常{
新的TestUtils.TestDataInitter(InstrumentationRegistry.getTargetContext())
.clearPrefs(真)
.acceptIntro(正确)
.initDatabaseFake(假)
.init();
}
}

您可以通过扩展并覆盖
onCreate
来创建
MyApplication
的测试版本。您的首选项是否更改,然后调用
super.onCreate
。在插装测试期间使用此
MyTestApplication
。我希望从插装测试的角度覆盖该行为
public class SearchScreenFragmentTest {
    @Rule
    public ActivityTestRule activityRule = new ActivityTestRule<>(
            SearchActivity.class,
            true,    // initialTouchMode
            false);  // launchActivity. False to set intent per method

    public SearchScreenFragmentTest() {
    }

    @Before
    public void setUp() throws Exception {
        //gets run AFTER `MyApplication.onCreate()`. I want this to be called BEFORE.
        new TestUtils.TestDataInitter(InstrumentationRegistry.getTargetContext())
                .clearPrefs(true)
                .acceptIntro(true)
                .initDatabaseFake(true)
                .init();
    }

    @After
    public void tearDown() throws Exception {
        new TestUtils.TestDataInitter(InstrumentationRegistry.getTargetContext())
                .clearPrefs(true)
                .acceptIntro(true)
                .initDatabaseFake(false)
                .init();
    }

}