1

I am trying to write testcase for room database in kotlin for Android. With below implementation, error occurs

DummyDatabase.kt

    internal abstract class DummyDatabase : RoomDatabase() {
    companion object {
        
        @Volatile
        private var INSTANCE: DummyDatabase ? = null

        fun getInstance(context: Context): DummyDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context,
                    DummyDatabase::class.java,
                    "db_name.db"
                ).build()
                INSTANCE = instance
                instance
            }
        }
    }
}

Error:

Method getWritableDatabase in android.database.sqlite.SQLiteOpenHelper not mocked. See http://g.co/androidstudio/not-mocked for details.
java.lang.RuntimeException: Method getWritableDatabase in android.database.sqlite.SQLiteOpenHelper not mocked. See http://g.co/androidstudio/not-mocked for details.
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java)


  public void update(final ComplicationRoutine complicationRoutine) {
    __db.assertNotSuspendingTransaction();

How to create mock for SQLiteOpenHelper in Room Database?

2
  • room has a specific dependency for testing, isn't it ? isn't there an in memory room db you can try ? Commented Jan 25, 2024 at 6:17
  • Room database makes use of SupportSQLiteOpenHelper, then why would error for SQLiteOpenHelper occurrs? Commented Jan 25, 2024 at 7:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.