Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not Able to mock methods called inside init block of a class #1142

Open
rakshushetty opened this issue Sep 8, 2023 · 1 comment
Open

Not Able to mock methods called inside init block of a class #1142

rakshushetty opened this issue Sep 8, 2023 · 1 comment

Comments

@rakshushetty
Copy link

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.

@Raibaz
Copy link
Collaborator

Raibaz commented Oct 3, 2023

init blocks cannot be mocked.

Out of curiosity, why is your fetchAllApis() call in an init block rather than in a constructor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants