我使用刀柄进行依赖注入。
我使用这篇文章进行工具测试。
https://developer.android.com/training/dependency-injection/hilt-testing
https://dagger.dev/hilt/testing.html
现在想为我的片段编写测试。
我在添加库时遇到问题。
这是我的代码
这个课程是我的跑步者,在 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 类中使用的应用程序类
@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
答案 0 :(得分:1)
我以前遇到过这个问题。出现这个问题是因为版本不兼容。
使用:
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")