# ViewModelScenario

Artifact: [androidx.lifecycle:lifecycle-viewmodel-testing](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/viewmodel/testing/ViewModelScenario.kt+class:androidx.lifecycle.viewmodel.testing.ViewModelScenario) Added in [2.9.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.9.0)

*** ** * ** ***


```
public final class ViewModelScenario<VM extends ViewModel> implements AutoCloseable
```

<br />

*** ** * ** ***

`https://developer.android.com/reference/androidx/lifecycle/viewmodel/testing/ViewModelScenario` provides API to start and drive a `https://developer.android.com/reference/androidx/lifecycle/ViewModel`'s lifecycle state for testing.

`https://developer.android.com/reference/androidx/lifecycle/viewmodel/testing/ViewModelScenario#recreate()` allows you to simulate a System Process Death and restoration.

`https://developer.android.com/reference/androidx/lifecycle/viewmodel/testing/ViewModelScenario` does not clean up the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` automatically. Call `https://developer.android.com/reference/androidx/lifecycle/viewmodel/testing/ViewModelScenario#close()` in your test to clean up the state or use `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/index.html` to ensure `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore#clear()` and `https://developer.android.com/reference/androidx/lifecycle/ViewModel#onCleared()` is invoked.

## Summary

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/lifecycle/viewmodel/testing/ViewModelScenario#close()()` Finishes the managed `https://developer.android.com/reference/androidx/lifecycle/ViewModel` and clear the `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull VM` | `https://developer.android.com/reference/androidx/lifecycle/viewmodel/testing/ViewModelScenario#getViewModel()()` The current `https://developer.android.com/reference/androidx/lifecycle/ViewModel` being managed by this scenario. |
| `final void` | `https://developer.android.com/reference/androidx/lifecycle/viewmodel/testing/ViewModelScenario#recreate()()` Simulates a System Process Death recreating the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` and all associated components. |

## Public methods

### close

Added in [2.9.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.9.0)

```
public void close()
```

Finishes the managed `https://developer.android.com/reference/androidx/lifecycle/ViewModel` and clear the `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore`.

### getViewModel

Added in [2.9.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.9.0)

```
public final @NonNull VM getViewModel()
```

The current `https://developer.android.com/reference/androidx/lifecycle/ViewModel` being managed by this scenario. This instance is change if the `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore` is cleared or if `https://developer.android.com/reference/androidx/lifecycle/viewmodel/testing/ViewModelScenario#recreate()` is invoked.

### recreate

Added in [2.9.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.9.0)

```
public final void recreate()
```

Simulates a System Process Death recreating the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` and all associated components.

This method:

- Saves the state of the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` with `https://developer.android.com/reference/androidx/savedstate/SavedStateRegistryController#performSave(androidx.savedstate.SavedState)`.

- Recreates the `https://developer.android.com/reference/androidx/lifecycle/ViewModelStoreOwner`, `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` and `https://developer.android.com/reference/androidx/savedstate/SavedStateRegistryOwner`.

- Recreates the `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` instance.

Call this method to verify that the `https://developer.android.com/reference/androidx/lifecycle/ViewModel` correctly preserves and restores its state.