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

Michael Vandendriessche

02/24/2022, 2:48 PM
Hi everyone! I'm having some trouble setting up unit tests for Android in Kotest and MockK. I recently joined a company who already has an android application which did not contain any unit tests. The code is not made with testability in mind so I'm having some trouble. I recently fixed something in a Fragment so I improved the code but I wouldn't call it great. I would like to write tests for it now. Currently I want to test a kotlin function which calls code like this. The other classes are in Java.
ClassWithManyStaticMethods.getSomething().method(lateInitialisedField)
I finally figured out how to mock the static class but stumble upon following error
lateinit property lateInitialisedField has not been initialized
. So the
lateInitialisedField
is a lateinit in the class I want to test. When I call the function I want to test, the
lateInitialisedField
is not initialised. How do I initialise it in the test or prevent the error? The value does not matter for my test, I just want to verify if
method
has been called. I tried using
@InjectMocks
when I create the class under test and created a mockk
lateInitialisedField
in my test but either I'm doing something wrong or this is not the solution. If anyone could point me in the right direction that would be greatly appreciated!
m

Mattia Tommasone

02/24/2022, 2:55 PM
how is
lateInitializedField
being initialized? is it publicly accessible, i.e. can you initialize it in your test?
m

Michael Vandendriessche

02/24/2022, 4:16 PM
@Mattia Tommasone It is private and is initialised in a private function called by the android lifecycle method
onCreateView
. Initialised with a property on the same class as above
ClassWithManyStaticMethods.getSomething().someProperty
It's a unit test so the onCreateView is never called.
4 Views