I'm doing an app in Android/Kotlin as a portfolio, and I want to use navigation testing to do UI tests. But the app also has Hilt and I had to configure both to run the tests, using Hilt android testing.
When doing the tests, I had a series of problems, and I ended up getting stuck in this one:
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)
This error does not run the app screen composable (the composable that is in main activity) in the test. The code is like this:
@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() function, which runs the screen composable:
@Composable
fun preparacaoDosTestes(): TestNavHostController {
val navController = TestNavHostController(context = LocalContext.current)
navController.navigatorProvider.addNavigator(ComposeNavigator())
TelaApp(navController)
return navController
}
My Main Activity:
@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)
}
}
}
}
}
I found similar issues in some forums, like this one, another, and last one. Some of them saying about a navigation version compatibility issue with hilt. I had tried changing the versions and it still didn't work.
my 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")
}
How can I fix this error? and in my case, is it really the version incompatibility of both libraries?
Ps: my project on github to take a better look at the problem