Hilt's assisted factories are final, therefore, when we want to change the viewmodel binding module to its test version, we can't create an inner anonymous class.
So how are we supposed to mock the viewmodels injected by the Hilt?
Project Viewmodel binding module:
@InstallIn(ActivityRetainedComponent::class)
@Module
interface ActivityModule
{
@Binds
@IntoMap
@StringKey("my.package.MyViewModel")
fun bindMyViewModel(factory: MyViewModel_AssistedFactory): ViewModelAssistedFactory<out ViewModel>
}
Fragment Test Code Where I Want to Mock the Viewmodel:
@HiltAndroidTest
@UninstallModules(ActivityModule::class)
class FragmentTest
{
@get:Rule
val hiltRule = HiltAndroidRule(this)
private val viewModelFactory : MyViewModel _AssistedFactory= object : MyViewModel _AssistedFactory {
//ERROR: This type is final, so it cannot be inherited from
}
}