ansman/auto-dagger

Applying the android library makes it difficult to write Compose UI tests

Closed this issue · 1 comments

Describe the bug

Adding the se.ansman.dagger.auto:android seems to effectively force you to run your Application to implement GeneratedComponentManagerHolder. However, when writing pure Compose tests, this is likely not the case.

To Reproduce

Create an Android library module (e.g. :testing) and add the se.ansman.dagger.auto:android library as an implementation dependency. Then, create a simple Compose Test using createComposeRule():

class TextTest {
    @get:Rule
    val composeRule = createComposeRule()

    @Test
    fun textIsVisible() {
        composeRule.setContent {
            Text("Text label")
        }

        composeRule.onNodeWithText("Text label").assertExists()
    }
}

Expected behavior

Running ./gradlew :testing:connectedAndroidTest should run without any problems.

Actual behavior

The following exception is thrown (newlines added for readability):

java.lang.RuntimeException: Unable to get provider androidx.startup.InitializationProvider:
androidx.startup.StartupException: java.lang.IllegalStateException:
Expected application to implement GeneratedComponentManagerHolder.
Check that you're passing in an application context that uses Hilt.
Application class found: class android.app.Application

Environment

  • Kotlin Version: 1.9.23
  • KSP or KAPT: KSP
  • KSP version: 1.9.23-1.0.20
  • Dagger Version: 2.51.1
  • Hilt Version: 1.1.0

Additional context

Applying the se.ansman.dagger.auto:android-testing doesn't help. I didn't expect it to, either, but I figured it was worth trying.

Plugins applied:

  • com.android.library
  • com.google.dagger.hilt.android
  • com.google.devtools.ksp
  • org.jetbrains.kotlin.android

A new artifact, android-api, has been added which you can add to library modules instead.