Testing Android Functions That Launch Coroutines
Explore how to test Android components that launch Kotlin coroutines by replacing the Main dispatcher with a test dispatcher. Understand the use of StandardTestDispatcher, control coroutine timing with advanceTimeBy and advanceUntilIdle, and implement JUnit rules or extensions to manage test lifecycle and dispatcher setup. This lesson empowers you to write efficient and reliable tests for coroutine-based Android code.
We'll cover the following...
We typically start coroutines on ViewModels, Presenters, Fragments, or Activities on Android. These are fundamental classes, and we should test them. Think of the MainViewModel implementation below.
Instead of viewModelScope, there might be our own scope, and instead of ViewModel, it might be Presenter, Activity, or some other class. It does not matter for our example. As in every class that starts coroutines, we should use StandardTestDispatcher as a part of the scope. Previously, we needed to inject a different scope with a dependency ...