Android instrumentation test doesn't run to end when using Room @Transaction function

前端 未结 2 822
有刺的猬
有刺的猬 2020-12-19 12:28

I\'m testing with AS 3.4.1 and Emulator running Android 9.

The following test won\'t run, when I use a Room Dao Function annotated with @Transaction in

相关标签:
2条回答
  • 2020-12-19 12:49

    I faced the same issue and the problem was because of the InstantTaskExecutorRule, if you remove the below block of code the @Transaction should work fine with the suspend keyword

    @Rule
    @JvmField
    val instantTaskExecutorRule = InstantTaskExecutorRule()
    

    It seems that this rule blocks the RoomDatabase from acquiring the transaction thread. In RoomDatabase.kt execution gets blocked in the below function:

    private suspend fun Executor.acquireTransactionThread(controlJob: Job): ContinuationInterceptor
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-19 12:57

    You can use setTransactionExecutor to run transaction in another thread

    return Room
           .inMemoryDatabaseBuilder(context, MyRoomDatabase::class.java)
           .setTransactionExecutor(Executors.newSingleThreadExecutor())
           .build()
    

    then while testing use runBlocking instead of runBlockingTest

    @Test
    fun moveItem() = runBlocking {
        transactionFunction()
    }
    
    0 讨论(0)
提交回复
热议问题