找不到符号导入 dagger.hilt.android.internal.Contexts
cannot find symbol import dagger.hilt.android.internal.Contexts
我正在尝试为 Dagger_Hilt (hilt-android-testing:2.38.1) 编写测试,但不幸的是,这个错误发生在我运行测试了。我不明白这是什么问题。如果有人能帮我解决这个错误,那将很有帮助。
cannot find symbol
import dagger.hilt.android.internal.Contexts;
^
symbol: class Contexts location: package
dagger.hilt.android.internal
ShoppingDaoTest.kt class:
@ExperimentalCoroutinesApi
@SmallTest
@HiltAndroidTest
class ShoppingDaoTest {
@get:Rule
var hileRule = HiltAndroidRule(this)
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Inject
@Named("test_db")
lateinit var database: ShoppingItemDatabase
private lateinit var dao: ShoppingDao
@Before
fun setup() {
hileRule.inject()
dao = database.shoppingDao()
}
@After
fun teardown() {
database.close()
}
@Test
fun insertShoppingItem() = runBlockingTest {
val shoppingItem = ShoppingItem("name", 1, 1f, "url", id = 1)
dao.insertShoppingItem(shoppingItem)
val allShoppingItems = dao.observeAllShoppingItems().getOrAwaitValue()
assertThat(allShoppingItems).contains(shoppingItem)
}
}
TestModule.kt class :
@Module
@InstallIn(SingletonComponent::class)
object TestAppModule {
@Provides
@Named("test_db")
fun provideinMemoryDb(
@ApplicationContext context: Context
) = Room.inMemoryDatabaseBuilder(context, ShoppingItemDatabase::class.java)
.allowMainThreadQueries()
.build()
}
HiltTestRunner.kt class :
class HiltTestRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
// return super.newApplication(cl, className, context)
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
和 build.gradle 设置:
android {
defaultConfig {
...
testInstrumentationRunner "com.mypackage.name.HiltTestRunner"
}
}
dependencies{
...
// Local Unit Tests
implementation "androidx.test:core:1.4.0"
testImplementation "junit:junit:4.13.2"
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.2.1"
testImplementation "com.google.truth:truth:1.0.1"
testImplementation "org.mockito:mockito-core:2.21.0"
// Instrumented Unit Tests
androidTestImplementation "junit:junit:4.13.2"
androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:2.12.1"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.2.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.2.0'
androidTestImplementation "org.mockito:mockito-core:2.21.0"
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'
}
我遇到了同样的问题,查看我的代码,我注意到我的 dagger-hilt 类路径项目在 2.36 上,所以我更新到 2.38.1 并检查所有刀柄依赖项是否具有相同的版本(否则错误一再发生) 最后你必须清除并重建项目。我希望这个解决方案对你有用!
类路径插件:
classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
匕首柄:
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
implementation "com.google.dagger:hilt-android:2.38.1"
匕首柄测试:
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'
我正在尝试为 Dagger_Hilt (hilt-android-testing:2.38.1) 编写测试,但不幸的是,这个错误发生在我运行测试了。我不明白这是什么问题。如果有人能帮我解决这个错误,那将很有帮助。
cannot find symbol import dagger.hilt.android.internal.Contexts; ^
symbol: class Contexts location: package dagger.hilt.android.internal
ShoppingDaoTest.kt class:
@ExperimentalCoroutinesApi
@SmallTest
@HiltAndroidTest
class ShoppingDaoTest {
@get:Rule
var hileRule = HiltAndroidRule(this)
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
@Inject
@Named("test_db")
lateinit var database: ShoppingItemDatabase
private lateinit var dao: ShoppingDao
@Before
fun setup() {
hileRule.inject()
dao = database.shoppingDao()
}
@After
fun teardown() {
database.close()
}
@Test
fun insertShoppingItem() = runBlockingTest {
val shoppingItem = ShoppingItem("name", 1, 1f, "url", id = 1)
dao.insertShoppingItem(shoppingItem)
val allShoppingItems = dao.observeAllShoppingItems().getOrAwaitValue()
assertThat(allShoppingItems).contains(shoppingItem)
}
}
TestModule.kt class :
@Module
@InstallIn(SingletonComponent::class)
object TestAppModule {
@Provides
@Named("test_db")
fun provideinMemoryDb(
@ApplicationContext context: Context
) = Room.inMemoryDatabaseBuilder(context, ShoppingItemDatabase::class.java)
.allowMainThreadQueries()
.build()
}
HiltTestRunner.kt class :
class HiltTestRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
// return super.newApplication(cl, className, context)
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
和 build.gradle 设置:
android {
defaultConfig {
...
testInstrumentationRunner "com.mypackage.name.HiltTestRunner"
}
}
dependencies{
...
// Local Unit Tests
implementation "androidx.test:core:1.4.0"
testImplementation "junit:junit:4.13.2"
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.2.1"
testImplementation "com.google.truth:truth:1.0.1"
testImplementation "org.mockito:mockito-core:2.21.0"
// Instrumented Unit Tests
androidTestImplementation "junit:junit:4.13.2"
androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:2.12.1"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.2.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.2.0'
androidTestImplementation "org.mockito:mockito-core:2.21.0"
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'
}
我遇到了同样的问题,查看我的代码,我注意到我的 dagger-hilt 类路径项目在 2.36 上,所以我更新到 2.38.1 并检查所有刀柄依赖项是否具有相同的版本(否则错误一再发生) 最后你必须清除并重建项目。我希望这个解决方案对你有用!
类路径插件:
classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
匕首柄:
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
implementation "com.google.dagger:hilt-android:2.38.1"
匕首柄测试:
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.38.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.38.1'