Search code examples
androidjunitdagger-hiltscreenshot-testingandroid-testify

How can I run android-testify tests with Dagger Hilt?


I am attempting to integrate https://github.com/Shopify/android-testify to add screenshot testing to my app. I am having trouble, however, with getting the simplest of tests to run due to my app's current integration with Dagger Hilt.

If I try to add only the ScreenshotRule as suggested by Testify's documentation, I get the error:

Caused by: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.

Additionally, if I use both the standard hilt rule (HiltAndroidRule(this)) alongside the ScreenshotRule, the tests just fail immediately, including tests that worked before adding the ScreenshotRule.


Solution

  • I don't have any first-hand experience with Hilt and Testify, but I do have some experience with mixing multiple test Rule instances together. The issue there is that each rule assumes that it's going to be the only test rule running in a given test class. So, declaring multiple Rule instances can have them compete with one another and leave them in a state where they just fail immediately.

    In order to get the rules to work together, you may need to use a ruleChain.

    https://junit.org/junit4/javadoc/4.12/org/junit/rules/RuleChain.html

    This article on developer.android.com demonstrates a couple of techniques for dealing with multiple TestRule objects in your tests: https://developer.android.com/training/dependency-injection/hilt-testing#multiple-testrules