Espresso idling resource not working if long runni

2019-08-12 08:11发布

问题:

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