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

2

10

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 it.

class RecurrenceManagerTest : DatabaseTest() {

    @Rule
    @JvmField
    val instantTaskExecutorRule = InstantTaskExecutorRule()

    var recurringEntryId: Long = -1

    @Before
    override fun setup() {
        super.setup() // only initialized the db

        val recurringEntry = RecurringEntry(
            recurrence = Recurrence(DATE.toEpochMilli(), Recurrence.DAILY)
        )

        recurringEntryId = runBlocking { db.recurringEntryDao().insert(recurringEntry) }

        val recurringBookEntry = BookEntry.create(
            title = TITLE,
            date = DATE,
            value = VALUE,
            isPaid = IS_PAID,
            notes = NOTES,
            entryType = ENTRY_TYPE,
            categoryId = CATEGORY_ID,
            contacts = CONTACTS,
            recurringEntryId = recurringEntryId
        )

        runBlocking {
            db.bookEntryDao().insert(recurringBookEntry) // BreakPoint #1
        }
    }

    @Test
    fun testInsertRecurrencesAndSchedule() { 
        var recurringEntry = runBlocking { db.recurringEntryDao().get(recurringEntryId) } // BreakPoint #2

        assertThat(recurringEntry, notNullValue())

        runBlocking { RecurrenceManager.insertRecurrencesAndSchedule(ApplicationProvider.getApplicationContext(), db, recurringEntry!!) }

        val bookEntries = db.bookEntryDao().getBookEntries().liveDataValue()
    }
}

This is the function for inserting:

@Transaction
suspend fun insert(bookEntry: BookEntry): Long {
    val id = insert(bookEntry.entity)
    bookEntry.embeddedContacts?.apply {
        forEach {
            it.id = 0
            it.bookEntryId = id
        }
    }?.let {
        insert(it)
    }

    return id
}

So if I'm running the test like it is (see BreakPoint #1), BreakPoint #2 won't even be called, so the test ends somewhere before, without a result.

If I'm replacing the code at BreakPoint #1 with exact the same code, the insert function has, the test runs correctly.

Does anyone have an idea what's the issue here?

Lionize answered 30/5, 2019 at 14:26 Comment(4)
That's because you are assering without the runblocking has finishedKordofanian
@Stavro Xhardha I thought runblocking waits until the body has finished? Any way to solve this?Lionize
well I don't really know the way you are using. But check my post on the last section I am testing a room insert and select. check thisKordofanian
Answers on this related question by me suggest that this is not possible, at the moment :( #57028350Agata
A
27

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()
}
Atomicity answered 12/9, 2019 at 5:55 Comment(1)
Great! For users of kotlinx.coroutines 1.6.0: also works with runTestContradictory
A
6

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!

Aubree answered 7/6, 2019 at 23:34 Comment(6)
Well, the test runs now, but I need the InstantTaskExecutorRule for the liveDataValue() method, because after removing this line, an error is thrown: java.lang.IllegalStateException: Cannot invoke observeForever on a background thread.Lionize
How did you get it working with InstantTaskExecutorRule ?Aubree
The test runs, after removing InstantTaskExecutorRule, but now I've an error with observeForever.Lionize
My .liveDataValue() extension function is a copy of this one: github.com/googlesamples/android-architecture-components/blob/…Lionize
Is there a possibility to test LiveData + Coroutines?Lionize
I not sure to be honest. But I wasn't able to get it workingAubree

© 2022 - 2024 — McMap. All rights reserved.