Open JoseAlcerreca opened 2 years ago
Hi I take it same issue This issue reason composeTestRule.setContent { MainNavigation() } your function block MainNavigation() inside I assume that your code has viewModel = hiltViewModel(). compose navigation working issue is not generate hiltviewmodel() when test runtime . In mycase unfortunatally not working current version .
implementation "androidx.hilt:hilt-navigation-compose:$hiltComposeNavigation"
I managed to solve this by these two links:
Just leaving some info. We have wanted to create an convenient Hilt test activity but have been blocked on https://b.corp.google.com/issues/127986458. I don't think we're keen on using the workaround for this though as this is a convenience thing (since you can always define your own simple Hilt test activity) and not actually blocking.
I cannot access https://b.corp.google.com/issues/127986458 since is private. So what's its status?
I solved this issue by doing: Adding HiltComponentActivity to debug and adding it to the debug manifest:
HiltComponentActivity:
@AndroidEntryPoint class HiltComponentActitivity: ComponentActivity()
Then in my test:
@get:Rule val composeTestRule = createAndroidComposeRule<HiltComponentActivity>()
When creating Compose tests, it's very common to start an empty ComponentActivity so that you can set the content later:
However, this test crashes* because
ComponentActivity
is not annotated for Hilt. The workaround is to create aHiltComponentActivity
indebug/
and add it to the manifest, which is not ideal.We solved a very similar issue for Compose Testing already with the
ui-test-manifest
artifact: https://maven.google.com/web/index.html?q=manifest#androidx.compose.ui:ui-test-manifest:1.2.0-beta01which is installed in the debug sourceset:
For consistency, the new artifact could be called
ui-test-hilt-manifest
.