Carlos Daniel, In addition, Google provides a LiveData util., LiveDataTestUtil.kt, sample in the Android Testing Codelab Unit Testing with Coroutines section. It looks promising, but I have yet to test it in the wild so I cannot properly compare it yet.


import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
object LiveDataTestUtil {
* Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds.
* Once we got a notification via onChanged, we stop observing.
fun <T> getValue(liveData: LiveData<T>): T {
val data = arrayOfNulls<Any>(1)
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(o: T?) {
data[0] = o
latch.await(2, TimeUnit.SECONDS)

return data[0] as T

DeFi Punk