Jetpack系列(十) — 测试 ,2021Android高级面试题总结

 ```
Room 测试
  1. androidTest 文件夹下新建测试类```
    @RunWith(AndroidJUnit4::class)
    class WordDaoTest {
    private lateinit var database: AppDataBase
    private lateinit var wordDao: WordDao

    private val word1 = Word("hello")
    private val word2 = Word("world")
    
    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()
    
    @Before
    fun createDb() = runBlocking {
        val context = InstrumentationRegistry.getInstrumentation().targetContext
        database = Room.inMemoryDatabaseBuilder(context, AppDataBase::class.java).build()
        wordDao = database.wordDao()
    
        wordDao.insertAll(listOf(word1, word2))
    }
    
    @After
    fun closeDb() {
        database.close()
    }
    
    @Test
    fun testGetAlphabetizedWords() = runBlocking {
        val wordList = wordDao.getAlphabetizedWords().first()
        Assert.assertThat(wordLi

你可能感兴趣的:(Android,android,面试,java,移动开发)