Search code examples
androidunit-testingkotlinkotlinx.coroutines

Unit test with coroutines in kotlinMethod myLooper in android.os.Looper not mocked error when I try to Unit test in kotlin


I am getting Method myLooper in android.os.Looper not mocked error when I try to test my ViewModel in kotlin using corountines.

Theres is the ViewModel

class MainViewModel(private val uiContext: CoroutineContext = Dispatchers.Main) : ViewModel(), CoroutineScope {

    private val heroesRepository: HeroesRepository = heroesRepositoryModel.instance()
    val data = MutableLiveData<List<Heroes.Hero>>()

    private var job: Job = Job()
    override val coroutineContext: CoroutineContext
        get() = uiContext + job

    fun getHeroesFromRepository(page: Int) {
        launch {
            try {
                val response = withContext(Dispatchers.IO) {
                    heroesRepository.getHeroes(page).await()
                }
                data.value = response.data.results
            } catch (e: HttpException) {
                data.value = null
            } catch (e: Throwable) {
                data.value = null
            }
        }
    }

    override fun onCleared() {
        super.onCleared()
        job.cancel()
    }
}

And the Test that I made for this ViewModel

class HeroesDataSourceTest {

    @Mock
    lateinit var heroesRepository: HeroesRepository

    @Mock
    lateinit var deferred: Deferred<Heroes.DataResult>
    val hero = Heroes.Hero(1, "superman", "holasuperman", 1, null, null)
    val results = Arrays.asList(hero)
    val data = Heroes.Data(results)
    val dataResult = Heroes.DataResult(data)



    @Before
    fun initTest() {
        MockitoAnnotations.initMocks(this)
    }

    @Test
    fun testLoadInitialSuccess(): Unit = runBlocking {
        `when`(heroesRepository.getHeroes(0)).thenReturn(deferred)
        `when`(deferred.await()).thenReturn(dataResult)
        var liveData: MutableLiveData<List<Heroes.Hero>>
        val mainViewModel = MainViewModel(Dispatchers.Unconfined)
        liveData = mainViewModel.data
        mainViewModel.getHeroesFromRepository(0)
        delay(10000L)
        Assert.assertEquals(dataResult, liveData.value)
    }

}

I debug it and it gives me the error in the line data.value = response.data.results of the ViewModel. It goes to the exception but for sure as the data is empty the assertEquals is going to be false.

I checked this thread: Method myLooper in android.os.Looper not mocked with Coroutines

And also this solution: https://android.jlelse.eu/mastering-coroutines-android-unit-tests-8bc0d082bf15

That is working but in kotlin 1.3 kotlinx.coroutines.experimental.android.UI doesn't work.


Solution

  • LiveData uses MainLooper internally. Add this dependency(or its support library version):

    testImplementation "androidx.arch.core:core-testing:$lifecycle_version"

    and this rule:

    @get:Rule
    val instantExecutorRule = InstantTaskExecutorRule()
    

    https://developer.android.com/reference/android/arch/core/executor/testing/InstantTaskExecutorRule