Testing Android Room with LiveData, Coroutines and Transactions

前端 未结 2 1239

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条回答
  •  萌比男神i
    2021-02-19 12:04

    There is now a solution to this issue, explained in this answer.

    The fix is adding a single line to the Room in-memory database builder:

    db = Room
        .inMemoryDatabaseBuilder(context, AppDatabase::class.java)
        .setTransactionExecutor(Executors.newSingleThreadExecutor()) // <-- this makes all the difference
        .build()
    

    With the single thread executor the tests are working as expected.

提交回复
热议问题