Home > OS >  Android Unit test with Room database error
Android Unit test with Room database error

Time:01-24

Writing unit test to insert object to room database, take it from database and assert are the 2 mentioned equal. Here is the code:

@RunWith(AndroidJUnit4::class)
@SmallTest
class GdprDaoTest {

    private lateinit var dao: GdprDao
    private lateinit var database: AppDatabase

    @get:Rule
    val instantTaskExecutorRule = InstantTaskExecutorRule()

    @Before
    fun setup() {
        database = Room.inMemoryDatabaseBuilder(
            ApplicationProvider.getApplicationContext(),
            AppDatabase::class.java
        ).allowMainThreadQueries().build()


        dao = database.gdprDao()
    }


    @Test
    fun insertConfiguration(){
        runBlocking {
            val insertedGdprEntity =
                GdprEntity(id = 1, conditionsUrl = "conditionsUrl", agreementUrl = "AgreementUrl")
            dao.insert(insertedGdprEntity)

            var fetchedGdprEntity = dao.getGdpr().getOrAwaitValue()

            assertEquals(insertedGdprEntity, fetchedGdprEntity)


        }
    }

    @After
    fun tearDown() {
        database.close()
    }

}

But this does not work. When I debug, it fails on the line dao.insert(insertedGdprEntity). In debug I can see that do is instantiated (it is not null). Looks pretty straight forward code to me, am I missing something? Here is the error I get from stacktrace:

System.logW: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.java.lang.Throwable: Explicit termination method 'close' not called

com.almworks.sqlite4java.SQLiteException: [-91] cannot load library: java.lang.UnsatisfiedLinkError: Can't load library: /var/folders/2v/mk_0n_7d2tgc3hnv9kr4t57w0000gn/T/1674396103129-0/libsqlite4java.dylib
java.lang.RuntimeException: com.almworks.sqlite4java.SQLiteException: [-91] cannot load library: java.lang.UnsatisfiedLinkError: Can't load library: /var/folders/2v/mk_0n_7d2tgc3hnv9kr4t57w0000gn/T/1674396103129-0/libsqlite4java.dylib
    at org.robolectric.shadows.util.SQLiteLibraryLoader.loadFromDirectory(SQLiteLibraryLoader.java:92)
    at org.robolectric.shadows.util.SQLiteLibraryLoader.doLoad(SQLiteLibraryLoader.java:55)
    at org.robolectric.shadows.util.SQLiteLibraryLoader.load(SQLiteLibraryLoader.java:39)
    at org.robolectric.shadows.ShadowSQLiteConnection.nativeOpen(ShadowSQLiteConnection.java:73)
    at android.database.sqlite.SQLiteConnection.nativeOpen(SQLiteConnection.java)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
    at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
    at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
    at android.database.sqlite.SQLiteDatabase.create(SQLiteDatabase.java:826)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:216)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
    at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:151)
    at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:112)
    at androidx.room.RoomDatabase.internalBeginTransaction(RoomDatabase.java:566)
    at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:555)
    at fortuna.vegas.android.data.local.database.dao.GdprDao_Impl$3.call(GdprDao_Impl.java:73)
    at fortuna.vegas.android.data.local.database.dao.GdprDao_Impl$3.call(GdprDao_Impl.java:70)
    at androidx.room.CoroutinesRoom$Companion$execute$2.invokeSuspend(CoroutinesRoom.kt:65)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at androidx.room.TransactionExecutor$1.run(TransactionExecutor.java:47)
    at androidx.arch.core.executor.testing.InstantTaskExecutorRule$1.executeOnDiskIO(InstantTaskExecutorRule.java:38)
    at androidx.arch.core.executor.ArchTaskExecutor.executeOnDiskIO(ArchTaskExecutor.java:96)
    at androidx.arch.core.executor.ArchTaskExecutor$2.execute(ArchTaskExecutor.java:53)
    at androidx.room.TransactionExecutor.scheduleNext(TransactionExecutor.java:61)
    at androidx.room.TransactionExecutor.execute(TransactionExecutor.java:54)
    at kotlinx.coroutines.ExecutorCoroutineDispatcherImpl.dispatch(Executors.kt:128)
    at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:322)
    at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
    at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
    at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:171)
    at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
    at androidx.room.CoroutinesRoom$Companion.execute(CoroutinesRoom.kt:64)
    at androidx.room.CoroutinesRoom.execute(CoroutinesRoom.kt)
    at fortuna.vegas.android.data.local.database.dao.GdprDao_Impl.insert(GdprDao_Impl.java:70)
    at fortuna.vegas.android.GdprDaoTest$insertConfiguration$1.invokeSuspend(GdprDaoTest.kt:55)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:279)
    at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
    at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
    at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
    at fortuna.vegas.android.GdprDaoTest.insertConfiguration(GdprDaoTest.kt:52)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:278)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: com.almworks.sqlite4java.SQLiteException: [-91] cannot load library: java.lang.UnsatisfiedLinkError: Can't load library: /var/folders/2v/mk_0n_7d2tgc3hnv9kr4t57w0000gn/T/1674396103129-0/libsqlite4java.dylib
    at app//com.almworks.sqlite4java.SQLite.loadLibrary(SQLite.java:97)
    at app//com.almworks.sqlite4java.SQLite.getSQLiteVersion(SQLite.java:114)
    at org.robolectric.shadows.util.SQLiteLibraryLoader.loadFromDirectory(SQLiteLibraryLoader.java:90)
    at org.robolectric.shadows.util.SQLiteLibraryLoader.doLoad(SQLiteLibraryLoader.java:55)
    at org.robolectric.shadows.util.SQLiteLibraryLoader.load(SQLiteLibraryLoader.java:39)
    at org.robolectric.shadows.ShadowSQLiteConnection.nativeOpen(ShadowSQLiteConnection.java:73)
    at android.database.sqlite.SQLiteConnection.nativeOpen(SQLiteConnection.java)
    at android.database.sqlite.SQLiteConnection.$$robo$$android_database_sqlite_SQLiteConnection$open(SQLiteConnection.java:209)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java)
    at android.database.sqlite.SQLiteConnection.$$robo$$android_database_sqlite_SQLiteConnection$open(SQLiteConnection.java:193)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java)
    at android.database.sqlite.SQLiteConnectionPool.$$robo$$android_database_sqlite_SQLiteConnectionPool$openConnectionLocked(SQLiteConnectionPool.java:463)
    at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java)
    at android.database.sqlite.SQLiteConnectionPool.$$robo$$android_database_sqlite_SQLiteConnectionPool$open(SQLiteConnectionPool.java:185)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java)
    at android.database.sqlite.SQLiteConnectionPool.$$robo$$android_database_sqlite_SQLiteConnectionPool$open(SQLiteConnectionPool.java:177)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java)
    at android.database.sqlite.SQLiteDatabase.$$robo$$android_database_sqlite_SQLiteDatabase$openInner(SQLiteDatabase.java:804)
    at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java)
    at android.database.sqlite.SQLiteDatabase.$$robo$$android_database_sqlite_SQLiteDatabase$open(SQLiteDatabase.java:789)
    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java)
    at android.database.sqlite.SQLiteDatabase.$$robo$$android_database_sqlite_SQLiteDatabase$openDatabase(SQLiteDatabase.java:694)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java)
    at android.database.sqlite.SQLiteDatabase.$$robo$$android_database_sqlite_SQLiteDatabase$openDatabase(SQLiteDatabase.java:669)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java)
    at android.database.sqlite.SQLiteDatabase.$$robo$$android_database_sqlite_SQLiteDatabase$create(SQLiteDatabase.java:826)
    at android.database.sqlite.SQLiteDatabase.create(SQLiteDatabase.java)
    at android.database.sqlite.SQLiteOpenHelper.$$robo$$android_database_sqlite_SQLiteOpenHelper$getDatabaseLocked(SQLiteOpenHelper.java:216)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java)
    at android.database.sqlite.SQLiteOpenHelper.$$robo$$android_database_sqlite_SQLiteOpenHelper$getWritableDatabase(SQLiteOpenHelper.java:164)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java)
    at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.$$robo$$androidx_sqlite_db_framework_FrameworkSQLiteOpenHelper_OpenHelper$getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:151)
    at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java)
    at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.$$robo$$androidx_sqlite_db_framework_FrameworkSQLiteOpenHelper$getWritableDatabase(FrameworkSQLiteOpenHelper.java:112)
    at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java)
    at androidx.room.RoomDatabase.internalBeginTransaction(RoomDatabase.java:566)
    at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:555)
    at fortuna.vegas.android.data.local.database.dao.GdprDao_Impl$3.call(GdprDao_Impl.java:73)
    at fortuna.vegas.android.data.local.database.dao.GdprDao_Impl$3.call(GdprDao_Impl.java:70)
    at androidx.room.CoroutinesRoom$Companion$execute$2.invokeSuspend(CoroutinesRoom.kt:65)
    at app//kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at androidx.room.TransactionExecutor$1.run(TransactionExecutor.java:47)
    at androidx.arch.core.executor.testing.InstantTaskExecutorRule$1.executeOnDiskIO(InstantTaskExecutorRule.java:38)
    at androidx.arch.core.executor.ArchTaskExecutor.executeOnDiskIO(ArchTaskExecutor.java:96)
    at androidx.arch.core.executor.ArchTaskExecutor$2.execute(ArchTaskExecutor.java:53)
    at androidx.room.TransactionExecutor.scheduleNext(TransactionExecutor.java:61)
    at androidx.room.TransactionExecutor.execute(TransactionExecutor.java:54)
    at kotlinx.coroutines.ExecutorCoroutineDispatcherImpl.dispatch(Executors.kt:128)
    at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:322)
    at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
    at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
    at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:171)
    at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
    at androidx.room.CoroutinesRoom$Companion.execute(CoroutinesRoom.kt:64)
    at androidx.room.CoroutinesRoom.execute(CoroutinesRoom.kt)
    at fortuna.vegas.android.data.local.database.dao.GdprDao_Impl.insert(GdprDao_Impl.java:70)
    at fortuna.vegas.android.GdprDaoTest$insertConfiguration$1.invokeSuspend(GdprDaoTest.kt:55)
    at app//kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:279)
    at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
    at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
    at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
    at fortuna.vegas.android.GdprDaoTest.insertConfiguration(GdprDaoTest.kt:52)
    at [email protected]/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at [email protected]/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at [email protected]/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at [email protected]/java.lang.reflect.Method.invoke(Method.java:566)
    ... 15 more
Caused by: java.lang.UnsatisfiedLinkError: Can't load library: /var/folders/2v/mk_0n_7d2tgc3hnv9kr4t57w0000gn/T/1674396103129-0/libsqlite4java.dylib
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2633)
    at java.base/java.lang.Runtime.load0(Runtime.java:768)
    at java.base/java.lang.System.load(System.java:1837)
    at com.almworks.sqlite4java.Internal.tryLoadFromPath(Internal.java:340)
    at com.almworks.sqlite4java.Internal.loadLibraryX(Internal.java:110)
    at com.almworks.sqlite4java.SQLite.loadLibrary(SQLite.java:95)
    ... 85 more

CodePudding user response:

As already discussed the solution is to increase the version of robolectric to 4.9 as described in this issue https://github.com/robolectric/robolectric/issues/7590

All that is needed to be done is update to:

testImplementation "org.robolectric:robolectric:4.9"

And:

testOptions { unitTests.includeAndroidResources = true }
  • Related