android - 未解析的引用 : HiltAndroidTest?

标签 android unit-testing testing android-testing dagger-hilt

我试图在使用 hilt 时进行测试,但是每当我添加任何 hilt-test 注释时,它都会给我这个错误。这是我的 gradle 文件

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'kotlin-android-extensions'
id "androidx.navigation.safeargs.kotlin"}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
  kotlinOptions {
      jvmTarget = '1.8'
   }
 }
kapt {
   correctErrorTypes true
}

dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
def hiltDaggerVersion = '2.29-alpha'

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

/* Testing */
// test kotlin
implementation 'androidx.test:core-ktx:1.3.0'
// fragment
debugImplementation "androidx.fragment:fragment-testing:1.2.5"
// hilt
androidTestImplementation "com.google.dagger:hilt-android-testing:2.29-alpha"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:2.29-alpha"
}

我在代码中使用 hilt 没有问题,但是当我想在测试中使用它时,android studio 无法识别注释或函数,直到我自己编写包名称,然后当我运行测试时它给了我这个错误。 有什么问题吗?

最佳答案

事实证明,我正在使用本地单元测试类的“仪器测试”依赖项,我应该像本指南那样使用“Robolectric 测试”Hilt testing guide

我正在学习教程,但他没有指出这一点(初学者的错误)。

关于android - 未解析的引用 : HiltAndroidTest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65144325/

相关文章:

android - 备份我的本地数据库/数据

java - android - 布局下的按钮仍然可以触摸

Android - 服务可以报告回调用 Activity 的状态吗

angularjs - Jasmine spy 方法返回未定义

testing - 找到 TestMain 多个定义

java - 如何对旧的遗留应用程序进行单元测试

android - 从 Android 中的 strings.xml 加载时出现空指针异常

unit-testing - 使用单元测试进行实际重构

java - 快速将assertXY语句从JUnit4迁移到Junit5?

ruby-on-rails-3 - 如何使用 Capybara 获取、显示、发布、放入 Controller 测试?