I'm excited to use the new Android Architecture Components ViewModel system, which nicely separates the Activity/Fragment/Layout rendering concerns from the ViewModel logic. I've successfully unit-tested the ViewModel in isolation, and now would like to try out some screenshot testing by providing mocked ViewModels to the Activity/Fragment for various state scenarios.
I've successfully configured my androidTests to be able to use Mockito in my device tests, that part works great.
However, the officially recommended way of calling ViewModelProvider or delegating by viewModels<>
does not seem to offer a way to inject mocked ViewModels. I'd rather not add an entire DI framework just to work around this omission in the documentation, so I wonder if anyone has any successful examples of providing mocked ViewModels with the official Android Architecture Components without the extra dependencies of Dagger or Hilt.
The only related answer from 1 year ago suggests using ActivityTestRule
and manually controlling the activity lifecycle, but that Rule is deprecated in favor of activityScenarioRule
which does not provide this control.