mockk/mockk
Not Able to mock methods called inside init block of a class
オープン
#1,142 opened on 2023/09/08
help wantedquestion
Repository metrics
- Stars
- (5,540 個のスター)
- PR merge metrics
- (平均マージ 4d 7h) (30d で 7 merged PRs)
説明
I have a case where i need to call 3 api's once Screen is launched. So I have written a method like this
fun fetchAllApis() {
fetchApi1()
fetchApi2()
fetchApi3()
}
and this method is called inside init block of ViewModel
init {
fetchAllApis()
}
Now the issue is, When i want to write unit test case for method fetchApi1() , I have to instantiate ViewModel which. internally calls init block and runs all the apis. I want to mock init block. When ever I create instance of ViewModel, It should not call fetchAllApis() method.
Is there any way i can achieve this?
I tried by using constructor mock, but did work.