Reputation: 32271
Consider the below example
@Singleton
class LoginModel @Inject contractor(private val userModel:UserModel){
}
@Config(application = HiltTestApplication::class)
@HiltAndroidTest
class LoginModelTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Inject
lateinit var loginModel: LoginModel
@Before
open fun setup() {
hiltRule.inject()
}
}
How to mock UserModel
inside the LoginModel
without manually constructing LoginModel
?
Upvotes: 5
Views: 4735
Reputation: 32271
Thanks, Jakub for the link. Here is my favorite way of doing it
@UninstallModules(AnalyticsModule::class)
@HiltAndroidTest
class SettingsActivityTest {
@BindValue @JvmField
val analyticsService: AnalyticsService = FakeAnalyticsService()
...
}
Upvotes: 0
Reputation: 794
The solution is simple. You have to deliver two different versions of UserModel - production, and mock in tests. You can do it by following these steps.
Replace that module just in single test sheet https://developer.android.com/training/dependency-injection/hilt-testing#replace-binding-manually
Or
Replace in all tests https://developer.android.com/training/dependency-injection/hilt-testing#replace-binding
In both cases you will end up with module which return mock in binding.
Cheers
Upvotes: 4