android - 添加库以进行刀柄测试的问题

标签 android testing dagger-hilt

我使用 hilt 进行依赖注入(inject)。
我使用这篇文章进行工具测试。
https://developer.android.com/training/dependency-injection/hilt-testing
https://dagger.dev/hilt/testing.html
现在想为我的 fragment 写测试。
我在添加库时遇到问题。
这是我的代码
这门课是我的运行者,在 gradle 中使用

class CustomTestRunner : AndroidJUnitRunner() {
    override fun newApplication(
        cl: ClassLoader?,
        className: String?,
        context: Context?
    ): Application {
        return super.newApplication(cl, MyCustom_Application::class.java.name, context)
    }
}
这是 hilt 生成我在 runner 类中使用的应用程序类的接口(interface)
@CustomTestApplication(AndroidApplication::class)
interface MyCustom
这是我的测试课
@HiltAndroidTest
class SettingsActivityTest {

    @get:Rule
    var hiltRule = HiltAndroidRule(this)

    // UI tests here.

    @Test
    fun test(){
//        val activityScenario = launchActivity<MainActivity>()
    }
}
我的图书馆
// For instrumented tests.
androidTestImplementation("com.google.dagger:hilt-android-testing:2.28-alpha")

// ...with Kotlin.
kaptAndroidTest("com.google.dagger:hilt-android-compiler:2.34.1-beta")

androidTestImplementation("junit:junit:4.12")
androidTestImplementation ("androidx.test.ext:junit:1.1.2")
androidTestImplementation ("androidx.test.espresso:espresso-core:3.3.0")
但我的问题:
当我添加这一行
kaptAndroidTest("com.google.dagger:hilt-android-compiler:2.34.1-beta")
gradle 显示此错误消息:
error: SettingsActivityTest_TestComponentDataSupplier is not abstract and does not override abstract method get() in TestComponentDataSupplier
public final class SettingsActivityTest_TestComponentDataSupplier extends TestComponentDataSupplier {
             ^
当我使用这条线时
kaptTest("com.google.dagger:hilt-android-compiler:2.34.1-beta")
android studio 无法生成 MyCustom_Application

最佳答案

我以前遇到过这个问题。出现此问题是由于版本不兼容。
使用:

androidTestImplementation("com.google.dagger:hilt-android-testing:2.34.1-beta")
kaptAndroidTest("com.google.dagger:hilt-android-compiler:2.34.1-beta")
implementation("com.google.dagger:hilt-android:2.34.1-beta") //hilt dependency
而不是这个:
androidTestImplementation("com.google.dagger:hilt-android-testing:2.28-alpha")
kaptAndroidTest("com.google.dagger:hilt-android-compiler:2.34.1-beta")

关于android - 添加库以进行刀柄测试的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68259101/

相关文章:

r - 如何测试没有发生错误?

c# - 在 windows phone 7 模拟器上模拟连接不稳定进行测试

android - 如果没有 @Provides 注释的方法,则无法提供 Dagger 柄

android - 具有多个接口(interface)实现的 Dagger Hilt

java - Android Activity "implements"无法解析类方法的符号?

android - Retrofit2 替换查询参数

java - 不支持的操作异常 : WebView is not allowed in privileged processes

Android限制CALL_PHONE权限避免隐藏来电

unit-testing - 在PHP中获取使用DomCrawler爬取的元素的Tag名称

android - 如何使用 Hilt 访问 attachBaseContext 中的注入(inject)属性?