Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hilt] FR: Create a HiltComponentActivity artifact #3394

Open
JoseAlcerreca opened this issue May 12, 2022 · 5 comments
Open

[Hilt] FR: Create a HiltComponentActivity artifact #3394

JoseAlcerreca opened this issue May 12, 2022 · 5 comments

Comments

@JoseAlcerreca
Copy link

When creating Compose tests, it's very common to start an empty ComponentActivity so that you can set the content later:

@HiltAndroidTest
class NavigationTest {

    @get:Rule(order = 0)
    var hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    val composeTestRule = createAndroidComposeRule<ComponentActivity>()

    @Test
    fun test1() {
        composeTestRule.setContent {
            MainNavigation()
        }
        composeTestRule.onNodeWithText("One")
    }
}

However, this test crashes* because ComponentActivity is not annotated for Hilt. The workaround is to create a HiltComponentActivity in debug/ and add it to the manifest, which is not ideal.

We solved a very similar issue for Compose Testing already with theui-test-manifest artifact:
https://maven.google.com/web/index.html?q=manifest#androidx.compose.ui:ui-test-manifest:1.2.0-beta01

which is installed in the debug sourceset:

debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")

For consistency, the new artifact could be called ui-test-hilt-manifest.


  • Crash error message:
Given component holder class androidx.activity.ComponentActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager
@nalanulusoy
Copy link

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"

@Chang-Eric
Copy link
Member

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.

@trietbui85
Copy link

trietbui85 commented Apr 13, 2024

I cannot access https://b.corp.google.com/issues/127986458 since is private. So what's its status?

@JoseAlcerreca
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants