Young Devs Bin
๐Ÿ“ Today I Learned

Testing Android MVVM Without Mocks

ยท1 min readยท#today-i-learned#android#kotlin#testing#mvvm
  • StateFlow with SharingStarted.WhileSubscribed doesn't emit until there's an active subscriber โ€” tests need backgroundScope.launch { vm.flow.collect {} } or the flow never starts
  • In-memory fakes (a MutableMap + MutableStateFlow) beat MockK for ViewModel tests: you test real state transitions, not just that a method was called
  • StandardTestDispatcher + advanceUntilIdle() gives you deterministic control over coroutines โ€” no delay(), no flaky timing
  • Writing the fakes also exposed bugs โ€” the TrashViewModel was creating a new MutableStateFlow() on every getDeletedNotes() call instead of returning the same flow, so upstream emissions never reached the ViewModel
  • 38 passing JVM tests, no device required, runs in ~3 seconds on CI