I have a function inside a class that is using a generic top level suspend function. In order to test this function I would need to mock this top level function, but so far I have not found a nice solution.
Lets say I have this:
suspend fun <T> awesoneGenericFunction(block: suspend (Bool) -> T): T {
complicatedCode()
return otherAwesomeCode(block)
}
With mockk it is possible to mock a static function by doing:
mockkStatic(::awesoneGenericFunction)
Sadly in this case this does not work because awesoneGenericFunction
is generic and the compiler is complaining that the type is missing. I know I can also do this:
mockkStatic("pckg.FileWithGenericFunctionKt")
coEvery { awesoneGenericFunction <Boolean>(any()) } returns false
This is working, but this approach does not feel right. Hard wiring the file(name) which contains the generic function seems like it could cause trouble in the future (eg if someone decide to move the function to some other file this test will fail. Also in this case the error message is somehow misleading which I believe will lead to some headache).
Is it possible to mock just the function without any further "wiring"?