簡體   English   中英

Android 房間數據庫錯誤的單元測試

[英]Android Unit test with Room database error

編寫單元測試以將 object 插入房間數據庫,從數據庫中取出並斷言上述 2 個相等。 這是代碼:

@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()
    }

}

但這不起作用。 當我調試時,它在 dao.insert(insertedGdprEntity) 行失敗。 在調試中,我可以看到 do 已實例化(它不為空)。 對我來說看起來很直接的代碼,我錯過了什么嗎? 這是我從堆棧跟蹤中得到的錯誤:

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 java.base@11.0.15/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base@11.0.15/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base@11.0.15/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base@11.0.15/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

如前所述,解決方案是將 robolectric 的版本增加到 4.9,如本期https://github.com/robolectric/robolectric/issues/7590中所述

所有需要做的就是更新為:

testImplementation "org.robolectric:robolectric:4.9"

和:

testOptions { unitTests.includeAndroidResources = true }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM