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.

LiveDataTestUtil.kt

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.countDown()
liveData.removeObserver(this)
}
}
liveData.observeForever(observer)
latch.await(2, TimeUnit.SECONDS)

@Suppress("UNCHECKED_CAST")
return data[0] as T
}
}

Creator of Coinverse - The 1st Crypto News Audiocast App @ bit.ly/play-coin

Creator of Coinverse - The 1st Crypto News Audiocast App @ bit.ly/play-coin