Simulate Activity/Fragment restore state in recreation with ViewModel using Espresso

Viewed 157

I`m using Espresso in Instrumented Tests of my mvvm app, and nearly everything is working just fine. But i want to cover the case of activity store/restore state during activity recreation. And i'm using this to simulate this scenario:

InstrumentationRegistry.getInstrumentation() .runOnMainSync { activity.recreate() }

And it works, onSaveInstanceState(outState: Bundle) is called and also onCreate(savedInstanceState: Bundle?) with savedInstanceState with my saved variables.

The problem is that onCleared of my ViewModel is not being called, because it's not being destroyed during the activity.recreate() call.

The main point here is when the Activity is recreated it's receiving the same ViewModel instance created earlier, and i don't want it to happen. I want it to work in same way when i manually turn Don't keep activities on.

Is there a way to simulate both Activity/Fragment and ViewModel being destroyed using Espresso?

0 Answers
Related