https://kotlinlang.org logo
#mockk
Title
# mockk
a

abhinaygupta

03/25/2024, 4:34 PM
I have a private variable of another class which is declared and initialised inside my class. this variable calls its class functions inside my current class functions internally. how to test this using MockK in kotlin? Since for testing , i require to mock this variable but it is private.
k

Klitos Kyriacou

03/25/2024, 6:24 PM
From a purist point of view, if you have a private variable in your class, that's implementation detail that you shouldn't try to mock. If you want to mock it, then it should be an external dependency that's injected preferably through a constructor. Having said that, there may be tricks available (probably using reflection) that can achieve exactly what you're after, but I wouldn't recommend that.
a

abhinaygupta

03/25/2024, 6:31 PM
Cool. Thanks Klitos. I would then make changes to my implementation.
3 Views