cucumber / cucumber-android

Android support for Cucumber-JVM
MIT License
135 stars 62 forks source link

Hilt and Cucumber tests #81

Closed sylwek845 closed 3 years ago

sylwek845 commented 3 years ago

Describe the bug Trying to run Dagger Hilt Rule and Cucumber tests, and it seems like the Hilt Rule is not getting invoked. Hilt Injection seems to be working without cucumber with exact the same setup.

Thats my custom runner

class ScreenshotTestRunner : CucumberAndroidJUnitRunner() {
    override fun onCreate(bundle: Bundle?) {
        super.onCreate(bundle)
        ScreenshotRunner.onCreate(this, bundle)
        ComposeScreenshotRunner.onCreate(this)
    }

    override fun onDestroy() {
        ScreenshotRunner.onDestroy()
        super.onDestroy()
    }

    override fun finish(resultCode: Int, results: Bundle?) {
        ScreenshotRunner.onDestroy()
        ComposeScreenshotRunner.onDestroy()
        super.finish(resultCode, results)
    }

    override fun newApplication(
        cl: ClassLoader,
        appName: String,
        context: Context
    ): Application {
        return super.newApplication(
            cl, HiltTestApplication::class.java.name, context
        )
    }
}

Additional context Add any other context about the problem here.

lsuski commented 3 years ago

Cucumber is built on top of Junit but in general it is recommended to use cucumber's @Before @After annotations. I think that junit rules will not work here - maybe they could work with some modifications in CucumberJUnitRunner. You can try to invoke hilt rule methods in @io.cucumber.java.Before and @io.cucumber.java.After hooks to simulate normal junit behaviour.

sylwek845 commented 3 years ago

@lsuski Thanks, I think the problem is that the rule needs Statement and Description which usually is taken from TestRule apply method. It would be good to have a Rule.

lsuski commented 3 years ago

Will be available in 4.9.0