找不到com.application.unittest.data.local.ShoppingItemDatabase的



我有一个房间测试的问题,我已经检查了所有的stackoverflow解决方案,但没有对我有效。我对考试还是个初学者。我尝试了很多解决方案,比如更改kapt并为room添加其他库

错误提示:

java.lang.RuntimeException: cannot find implementation for com.application.unittest.data.local.ShoppingItemDatabase. ShoppingItemDatabase_Impl does not exist
at androidx.room.Room.getGeneratedImplementation(Room.java:100)
at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1486)
at com.application.unittest.data.local.ShoppingDaoTest.setup(ShoppingDaoTest.kt:40)

这是我的DB类:

@Database(entities = [ShoppingItem::class], version = 1)
abstract class ShoppingItemDatabase:RoomDatabase() {
abstract fun shoppingDao():ShoppingDao
}

接口:

@Dao
interface ShoppingDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertShoppingItem(shoppingItem: ShoppingItem)
@Delete
suspend fun deleteShoppingItem(shoppingItem: ShoppingItem)
@Query("SELECT * FROM shopping_items")
fun observeAllShoppingItems():LiveData<List<ShoppingItem>>
@Query("SELECT SUM(price * amount) FROM shopping_items")
fun observeTotalPrice():LiveData<Float>
}

这是测试:

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
@SmallTest
class ShoppingDaoTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var database: ShoppingItemDatabase
private lateinit var dao: ShoppingDao
@Before
fun setup(){
database = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
ShoppingItemDatabase::class.java
)
.allowMainThreadQueries()
.build()
dao = database.shoppingDao()
}
@After
fun teardown(){
database.close()
}
@Test
fun insertShoppingItem() = runTest {
val shoppingItem = ShoppingItem(id = 1,"name",1,2f,"image")
dao.insertShoppingItem(shoppingItem)
val allShoppingItems = dao.observeAllShoppingItems().getOrAwaitValue()
assertThat(allShoppingItems).contains(shoppingItem)
}
}

那我该怎么办呢?

更新:

这是我的依赖项。我想我可能在我的依赖项中遗漏了一些东西:

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
//Material Design
implementation "com.google.android.material:material:1.6.1"
//Architectural Component
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
//Lifecycle
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime:2.4.1"
//Room
implementation "androidx.room:room-runtime:2.4.2"
implementation "androidx.room:room-ktx:2.4.2"
annotationProcessor "androidx.room:room-compiler:2.4.2"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
kaptAndroidTest "androidx.room:room-compiler:2.4.2"

//Kotlin extension and Coroutines for Room
implementation "androidx.room:room-ktx:2.4.2"
//Retrofit
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"

//Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1"
//Coroutines lifecycle scope
implementation "androidx.lifecycle:lifecycle-viewmodel:2.4.1"
//Navigation Component
implementation "androidx.navigation:navigation-fragment-ktx:2.4.2"
implementation "androidx.navigation:navigation-ui-ktx:2.4.2"
//Glide
implementation "com.github.bumptech.glide:glide:4.12.0"
kapt "com.github.bumptech.glide:compiler:4.12.0"
//Activity KTX for ViewModel
implementation "androidx.activity:activity-ktx:1.4.0"
//Dagger-Hilt
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
//Timber
implementation "com.jakewharton.timber:timber:5.0.1"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//Local Unit Test
implementation "androidx.test:core:1.4.0"
testImplementation "org.hamcrest:hamcrest-all:1.3"
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation "org.robolectric:robolectric:4.3.1"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1"
testImplementation "org.mockito:mockito-core:2.28.2"
testImplementation "com.google.truth:truth:1.0.1"
testImplementation "androidx.room:room-testing:2.4.2"
androidTestImplementation "com.google.truth:truth:1.0.1"
//Instrumentation Unit Test
androidTestImplementation "junit:junit:4.13.2"
androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:2.28.1"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1"
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation "com.google.truth:truth:1.0.1"
androidTestImplementation "androidx.test.ext:junit:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
androidTestImplementation "org.mockito:mockito-core:2.28.2"

您是否有一个应用程序类来实例化房间数据库?

如果是,请确保你的android manifest在application下有这行代码。

& lt;应用程序android: name ="com.application.unittest。(然后你的应用程序类名)">

我最近构建的应用程序与您的应用程序有相同的错误,但在我将应用程序名称放入清单后它工作了。我没有使用TDD或Hilt,所以我不确定这是否会起作用。

但是我使用的应用程序类包含了这个:

类MyApplication: Application() {

val database: your database class by lazy { your database class).getDatabase(this) }

}

然后确保应用程序名称在清单中。我希望这对你有帮助。

相关内容

最新更新