'Access ViewModel inside Testcase using Hilt

Can anyone suggest me that how can I access ViewModel inside Test case using Hilt?

ViewModel class:

@HiltViewModel
class BaseViewModel @Inject constructor(private val repository: BaseRepository) : ViewModel()

AppModule for Hilt

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @Provides
    fun provideBaseApi(
        remoteDataSource: RemoteDataSource
    ): BaseApi {
        return remoteDataSource.buildApi(BaseApi::class.java)
    }

Test case file

@SmallTest
@HiltAndroidTest
class BaseViewModelTest  {

    @get:Rule(order = 0)
    val hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    val activityRule = ActivityScenarioRule(MainActivity::class.java)

    @Inject
    lateinit var baseRepository: BaseRepository


    @BindValue
    @JvmField
    var viewModel = mockk<BaseViewModel>(relaxed = true)


    @Before
    fun init(){
        hiltRule.inject()
    }

It is not giving any access to mutable data of ViewModel class. Thank you in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source