Espresso idling resource not working if long running task is started in Activity's onCreate?

人盡茶涼 提交于 2019-12-11 08:57:06

问题


How to use espresso idling resource if long running task is started in Activity's onCreate ?

I have created a custom IdlingResource and it is working fine if the long async method call is triggered by click event, but breaks whenever the it is called in Acitivty's onCreate method.

Example:

public void onBtnClick(){
    setIdle(true); // This works fine, our tests wait until setIdle(false) is called
    doSomeBackgroundTask(); 
}

public void onDone(){
    setResourceIdle(false);
    setIdle(false);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setIdle(true); // This doesn't work, our tests won't wait
    doSomeBackgroundTask(); 
}

Any ideas to overcome this situation?


回答1:


Try registering your idlingResource before activity onCreate.

Simple @Before method should be enough.

You can also implement your own ActivityRule and override beforeActivityLaunched() method if you are using espresso-rules

Or if you want to execute any code before Application oncreate you need to implement your own testrunner and override callApplicationOnCreate()

Example of the test rule is shown here: ActivityTestRule - how to call code before Application's onCreate



来源:https://stackoverflow.com/questions/30968145/espresso-idling-resource-not-working-if-long-running-task-is-started-in-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!