How to set up the UI tests in a Hilt/Dagger App?

231 views Asked by At

I use Dagger in a project and have studying a bit whether migration to Hilt would make sense. My current setup is somewhat similar to the one presented in a (Hilt code lab)

i.e. I also have UserComponent with custom scope (from the moment the user logs in to the moment the user logs out). This is very handy as I have a lot’s of Repository classes caching user data and it’s very easy to cleanup all that data just by deleting the UserComponent. The migration strategy in my case would then be leaving Hilt and Dagger side by side.

In the Espresso tests I’m able to initialize the TestUserDataModule with useData that is needed for the test case. This makes it possible to launch Activity under test directly and make the app behave like there is a user already logged in. This part is not covered by the Hilt codelab or any other documentation I have seen so far.

So, how should I set up the UI tests in a project where Hilt/Dagger co-exist?

1

There are 1 answers

0
Andrew On

Refer to this tutorial, it explains how to implement UI / Fragment testing with Dagger hilt. The Codelab does not provide this, because the logic of the Ui test does not change when using hilt, but the approach does. Explaining everything in one Stackoverflow answer is way too much, so I hope the Video helps you.