Iman Sadrian
05/07/2022, 10:26 AM@HiltViewModel
open class BottomSheetViewModel @Inject constructor(
val savedStateHandle: SavedStateHandle,
val someRepository: SomeRepository,
)
where SomeRepository is:
@Singleton
open class SomeRepository@Inject constructor(
@ApplicationContext private val context:Context,
private val appLogManager: AppLogManager
)
so as you see one of the constructor parameters is savedStateHandle and because of that I can not inject that in test like this:
@Inject
lateinit var bottomSheetViewModel: BottomSheetViewModel
so as far as I see, seems the best practice for this is mocking, so I am trying to mock the viewModel like this:
@BindValue
val bottomSheetViewModel = mock<BottomSheetViewModel>()
Am I right so far?
but the problem is I get some errors when I create a mock from the viewModel
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.zabanshenas.tools.base.BaseViewModel.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Iman Sadrian
05/07/2022, 11:36 AM@Inject lateinit var someRepository: SomeRepository
@Before
fun setup() {
hiltRule.inject()
val savedStateHandle = SavedStateHandle()
bottomSheetViewModel = BottomSheetViewModel(
savedStateHandle = savedStateHandle,
someRepository = SomeRepository
)
}
Iman Sadrian
05/07/2022, 11:51 AMInjection of an @HiltViewModel class is prohibited since it does not create a ViewModel instance correctly.