I am trying to use idling resources in my test following this tutorial.
The problem is that the code to be tested already runs in the onCreate()
method.
In this (old) question it is suggested to register the idling resource in an @Before
annotated method, but that does not solve the problem since we still need an instance of the activity for the registering:
private ActivityScenario<MyActivity> scenario;
private IdlingResource myIdlingResource;
@Before
public void setUp() {
scenario = ActivityScenario.launch(MyActivity.class); // all the code in onCreate() (and also onStart() and onResume()) will run now and idling resource will be null
scenario.onActivity(activity -> {
// now we have a reference to the activity under test but it is already too late;
// this method can not be called before the activity is launched
myIdlingResource= activity.getIdlingResource();
IdlingRegistry.getInstance().register(myIdlingResource);
});
}
@Test
public void test() {
// assert stuff that happens when activity is created
}
How can I use idling resources in this scenario?