找不到com.application.unittest.data.local.local.shoppingitemdatabase的实现。 shoppitiTemdatabase_impl不存在
我对房间测试有问题,我检查了所有Stackoverflow解决方案,但对我没有任何帮助。 我是测试的初学者。 我尝试了很多解决方案,例如更改kapt,并为房间添加其他库
这是错误:
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)
这是我的数据库类:
@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"
I have a problem with Room test I have checked all stackoverflow solutions, but nothing works for me.
I'm so beginner at test.
I tried a lot of solution like change kapt and add other libraries for room
This is the Error:
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)
this is my DB class:
@Database(entities = [ShoppingItem::class], version = 1)
abstract class ShoppingItemDatabase:RoomDatabase() {
abstract fun shoppingDao():ShoppingDao
}
This is the interface:
@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>
}
This is the Test:
@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)
}
}
So what should I do?
UPDATE:
This is my dependencies. I guess maybe I missed something in my dependencies:
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否有应用程序类来实例化房间数据库?
如果是这样,请确保您的Android清单在应用程序中具有以下代码行。
&lt;应用
android:name =“ com.application.unittest。(然后您的应用程序类名称)”
Do you have an application class to instantiate the room database?
if so make sure your android manifest has this line of code under application.
<application
android:name="com.application.unittest.(then you application class name)"
我最近构建的应用程序的错误与您的错误相同,但是在将应用程序名称放在清单中后起作用。我没有使用TDD或刀柄,所以我不确定这是否会起作用。
但是我使用的应用程序类包含以下内容:
类myapplication:application(){
}
,然后确保应用程序名称在清单中。我希望这会有所帮助。
The app I built recently had the same error as yours but it worked after I put the application name in the manifest. I was not using TDD or Hilt though so I am not sure if this will work or not.
But the application class I used contained this:
class MyApplication: Application() {
}
Then make sure the application name is in the manifest. I hope this helps.