提问人:MuriTG25 提问时间:11/3/2023 更新时间:11/3/2023 访问量:60
给定组件持有者类 androidx.activity.ComponentActivity 未实现接口 dagger.hilt.internal.GeneratedComponent
Given component holder class androidx.activity.ComponentActivity does not implement interface dagger.hilt.internal.GeneratedComponent
问:
我正在做一个 Android/Kotlin 中的应用程序作为投资组合,我想使用导航测试来做 UI 测试。但是该应用程序也有 Hilt,我必须使用 Hilt android 测试将两者配置为运行测试。
在做测试时,我遇到了一系列问题,最终我陷入了这个问题:
java.lang.IllegalStateException: Given component holder class androidx.activity.ComponentActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager
//resto do erro oculto
at br.com.alura.mundobola.MainActivityKt.TelaApp(MainActivity.kt:149)
at br.com.alura.mundobola.auxiliardoteste.PreparacaoDosTestesKt.preparacaoDosTestes(PreparacaoDosTestes.kt:15)
at br.com.alura.mundobola.ui.screen.CadastroDeBolasScreenKtTest$preparaNavHostController$1.invoke(CadastroDeBolasScreenKtTest.kt:34)
at br.com.alura.mundobola.ui.screen.CadastroDeBolasScreenKtTest$preparaNavHostController$1.invoke(CadastroDeBolasScreenKtTest.kt:33)
此错误不会在测试中运行应用屏幕可组合项(主活动中的可组合项)。代码如下:
@HiltAndroidTest
class CadastroDeBolasScreenKtTest{
@get:Rule(order = 0)
val hiltRule = HiltAndroidRule(this)
@get:Rule(order = 1)
val testeDoNavigation = createComposeRule()
private lateinit var navController: NavHostController
@Before
fun preparaNavHostController(){
testeDoNavigation.setContent {
navController = preparacaoDosTestes()
hiltRule.inject()
}
}
@Test
fun deveMostrarFabENomeApp_QuandoEstiverNaTelaDeBolas(){
testeDoNavigation.onNodeWithText(tituloTelaLista).assertIsDisplayed()
//the rest I omitted
}
}
preparacaoDosTestes() 函数,用于运行屏幕可组合项:
@Composable
fun preparacaoDosTestes(): TestNavHostController {
val navController = TestNavHostController(context = LocalContext.current)
navController.navigatorProvider.addNavigator(ComposeNavigator())
TelaApp(navController)
return navController
}
我的主要活动:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MundoBolaTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val navController = rememberNavController()
TelaApp(navController)
}
}
}
}
}
我在一些论坛上发现了类似的问题,比如这个、另一个和最后一个。他们中的一些人说与刀柄的导航版本兼容性问题。我曾尝试更改版本,但仍然不起作用。
我的build.gradle(app):
//I only left the essentials on build.gradle
android {
defaultConfig {
testInstrumentationRunner = "br.com.alura.mundobola.ConfiguradorTesteHilt"
}
}
dependencies {
val navigationVersion = "2.7.3"
// https://mvnrepository.com/artifact/androidx.navigation/navigation-compose
implementation ("androidx.navigation:navigation-compose:$navigationVersion")
// https://mvnrepository.com/artifact/androidx.datastore/datastore-preferences
implementation ("androidx.datastore:datastore-preferences:1.0.0")
// https://mvnrepository.com/artifact/androidx.navigation/navigation-testing
androidTestImplementation ("androidx.navigation:navigation-testing:$navigationVersion")
val hiltVersion = "2.48"
implementation ("com.google.dagger:hilt-android:$hiltVersion")
kapt("com.google.dagger:hilt-compiler:$hiltVersion")
implementation ("androidx.hilt:hilt-navigation-compose:1.1.0")
androidTestImplementation("com.google.dagger:hilt-android-testing:$hiltVersion")
kaptAndroidTest("com.google.dagger:hilt-android-compiler:$hiltVersion")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
}
如何修复此错误?就我而言,真的是两个库的版本不兼容吗?
Ps:我在 github 上的项目可以更好地了解问题
答: 暂无答案
评论