Home > Software design >  Android unit testing kotlin: LiveData value was never set
Android unit testing kotlin: LiveData value was never set

Time:08-12

I'm getting this error while unit testing: "LiveData value was never set."

I'm trying to test the getStudents() method of my viewmodel that looks like this:

StudentViewModel:

@HiltViewModel
class StudentViewModel @Inject constructor(
    private val getStudentsUseCase: GetStudentsUseCase
) : ViewModel() {
    private val _studentLD = MutableLiveData<List<Student>>()
    val studentLD: LiveData<List<Student>> = _studentLD

    private val _errorLD = MutableLiveData<String>()
    val errorLD: LiveData<String> = _errorLD

    fun getStudents() {
        viewModelScope.launch {
            var result = getStudentsUseCase.execute()
            result.onSuccess {
                _studentLD.value = it
            }.onFailure {
                _errorLD.value = it.message.toString()
            }
        }
    }
}

StudentViewModelTest:

@OptIn(ExperimentalCoroutinesApi::class)
class StudentViewModelTest {

    private lateinit var viewModel: StudentViewModel

    @get:Rule
    var instantExecutorRule = InstantTaskExecutorRule()

    @get:Rule
    var mainCoroutineRule = MainCoroutineRule()

    @Mock
    private lateinit var getStudentsUseCase: GetStudentsUseCase

    private var studentList = listOf(
        Student("Max", "122d"),
        Student("Steven", "012s")
    )

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        viewModel = StudentViewModel(getStudentsUseCase)
    }

    @Test
    fun getStudentsSuccess() = runTest {
        Mockito.`when`(getStudentsUseCase.execute()).thenReturn(Result.success(studentList))

        viewModel.getStudents()
        val value = viewModel.studentLD.getOrAwaitValueTest()
        assertEquals(value, studentList)
    }
}

What Am I doing wrong? I also have a class for LiveDataTest with the method getOrAwaitValueTest() defined in there. Thank you in advance

Edit: Also, the getOrAwaitValueTest() method is defined here as follow:

@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun <T> LiveData<T>.getOrAwaitValueTest(
    time: Long = 2,
    timeUnit: TimeUnit = TimeUnit.SECONDS,
    afterObserve: () -> Unit = {}
): T {
    var data: T? = null
    val latch = CountDownLatch(1)
    val observer = object : Observer<T> {
        override fun onChanged(o: T?) {
            data = o
            latch.countDown()
            [email protected](this)
        }
    }
    this.observeForever(observer)

    try {
        afterObserve.invoke()

        // Don't wait indefinitely if the LiveData is not set.
        if (!latch.await(time, timeUnit)) {
            throw TimeoutException("LiveData value was never set.")
        }

    } finally {
        this.removeObserver(observer)
    }

    @Suppress("UNCHECKED_CAST")
    return data as T
}

CodePudding user response:

I found a solution to this: to make this working well, you need to add advanceUntilIdle() method.

Immediately execute all pending tasks and advance the virtual clock-time to the last delay. If new tasks are scheduled due to advancing virtual time, they will be executed before advanceUntilIdle returns.

@Test
fun getStudentsSuccess() = runTest {
        Mockito.`when`(getStudentsUseCase.execute()).thenReturn(Result.success(studentList))

        viewModel.getStudents()
        advanceUntilIdle()
        val value = viewModel.studentLD.getOrAwaitValueTest()

        assertEquals(value, studentList)
}
  • Related