Android Developers

Articles on modern tools and resources to help you build experiences that people love, faster and easier, across every Android device.

Unit Testing Lifecycle and State in ViewModels

Lifecycle 2.9.0-alpha01 introduced ViewModelScenario, a helper that simplifies unit testing for ViewModels.

2 min readMar 13, 2025

--

Why It Matters

You can test a ViewModel by simply creating an instance using its constructor in your test code. However, this approach has limitations — there is no straightforward way to:

With ViewModelScenario, these are now easy to test, helping you catch errors related to ViewModel clean-up and saved state.

How to Use

First, add the dependency to your Gradle file. It’s available from 2.9.0-alpha08:

implementation("androidx.lifecycle:lifecycle-viewmodel-testing:{version}")

Next, define your ViewModel:

class MyViewModel(
scope: CoroutineScope,
val handle: SavedStateHandle,
) : ViewModel(scope) { … }

/* Class used by SavedStateHandle. */
@Parcelize
data class MyData(
val name: String,
val surname: String,
) : Parcelable

Finally, write a unit test for your ViewModel using a ViewModelScenario:

class MyViewModelTest {

@Test
fun testStateRestoration() = runTest { // this: TestScope
viewModelScenario { // this: CreationExtras
MyViewModel(
scope = this@runTest,
handle = createSavedStateHandle(),
)
}.use { it: ViewModelScenario ->
// Set a ViewModel state.
it.viewModel.handle["key"] = MyData("John", "Doe")

// Recreate ViewModel, simulating a state restoration
// (including parcelizing).
it.recreate()

// Verify state is restored correctly after parcelizing.
assertThat(it.viewModel.handle["key"])
.isEqualTo(MyData("John", "Doe"))
}
}
}

Test in Details

We invite you to try out the new API and share any issues or feedback through our Lifecycle issue tracker.

Happy testing!

The code snippets in this blog have the following license:

// Copyright 2024 Google LLC. SPDX-License-Identifier: Apache-2.0

--

--

Android Developers
Android Developers

Published in Android Developers

Articles on modern tools and resources to help you build experiences that people love, faster and easier, across every Android device.

No responses yet