Sudhir Singh Khanger
05/27/2021, 4:06 PM@RunWith(AndroidJUnit4::class)
class SomeDaoTest {
private lateinit var database: AppDatabase
private lateinit var someDao: SomeDao
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Before
fun createDb() = runBlocking {
val context = InstrumentationRegistry.getInstrumentation().targetContext
database = Room
.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
// needed to run @Transaction block
.setTransactionExecutor(Executors.newSingleThreadExecutor())
.build()
someDao = database.someDao()
someDao.insertAll(dummy1, dummy2)
}
@After
fun closeDb() {
database.close()
}
@Test
fun testDeleteAll() = runBlocking {
val someData = someDao.getData().getOrAwaitValue()
someDao.deleteAll()
assertEquals(0, someData.size)
}
Any help on how I should do it or how to troubleshoot it would be nice.Chrimaeon
05/27/2021, 7:29 PMgetOrAwaitValue
is a suspend function and is „waiting“ for data to be reported. If you call the deleteAll
before the await it should work.Sudhir Singh Khanger
05/27/2021, 11:58 PMChrimaeon
05/29/2021, 6:24 AMrunBlocking
from Coroutines?Sudhir Singh Khanger
05/29/2021, 1:03 PM