Testing Android Room with LiveData, Coroutines and Transactions

前端 未结 2 1231

I want to test my database layer and I have caught myself in a catch-22 type of a situation.

The test case consists of two things:

  • Save so
2条回答
  •  情书的邮戳
    2021-02-19 11:40

    The problem is with the thing that transactions itself use runBlocking somewhere inside and that cause deadlock. I have changed InstantTaskExecutorRule to this class:

    class IsMainExecutorRule : TestWatcher() {
    
        val defaultExecutor = DefaultTaskExecutor()
    
        override fun starting(description: Description?) {
            super.starting(description)
            ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() {
                override fun executeOnDiskIO(runnable: Runnable) {
                    defaultExecutor.executeOnDiskIO(runnable)
                }
    
                override fun postToMainThread(runnable: Runnable) {
                    defaultExecutor.executeOnDiskIO(runnable)
                }
    
                override fun isMainThread(): Boolean {
                    return true
                }
            })
        }
    
        override fun finished(description: Description?) {
            super.finished(description)
            ArchTaskExecutor.getInstance().setDelegate(null)
        }
    }
    

    Then in code it will be:

    @get:Rule
    val liveDataRule = IsMainExecutorRule()
    

    It will not cause deadlocks but still allow to observe livedatas.

提交回复
热议问题