Android中Room Db的Junit测试用例无法正常工作

时间:2019-07-24 03:29:47

标签: android junit

我在android中运行Junit测试用例时遇到异常。 我正在为Room Database的Dao编写单元测试用例

android.arch.persistence.db.framework.FrameworkSQLiteDatabase.inTransaction

先谢谢了。

我已经关注了Android中Junit测试用例的在线教程。

@RunWith(JUnit4 :: class) Class BookDaoTest {

@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

@Mock
lateinit var bookDao: BookDao

@Mock
lateinit var bookDatabase: BookDatabase

@Mock
lateinit var application : Application

@Mock
lateinit var observer: Observer<List<Book>>

@Before
fun setUp() {
    MockitoAnnotations.initMocks(this)

    bookDatabase = Room.inMemoryDatabaseBuilder(this.application,BookDatabase::class.java)
            .allowMainThreadQueries().build();
    bookDao = bookDatabase.noteDao();

}

@Test
fun insert(){

    val book = Book("Testing Book",3f)
    //bookDao.getAllNotes().observeForever(observer);
    val userId=bookDao.insert(book)
    val userFromDb = bookDao.getAllNotes()

    Log.d("Here","VALUE"+bookDatabase.noteDao().insert(book))


    //bookDao?.insert(book)

  //  assertEquals(userId, 1)
  //  assertEquals(userFromDb?., user.userName)

  //  verify(observer).onChanged(Collections.singletonList(book))

}

@After
fun close(){
    bookDatabase.close()
}

}

0 个答案:

没有答案