Search code examples
kotlinmockkmockk-verify

How to verify a top level function was called with Mockk?


How to verify that a top level (static) function was called in a test with MockK?

Simple approach like:

verify { someTopLevelFunction("some text") }

results in io.mockk.MockKException: can't find stub kotlin.Unit.


Solution

  • You would mockk the same way you mockk extension functions https://mockk.io/#extension-functions

    In practice generally:

    mockkStatic(ClassNameWhereFunctionIsLocated::class)
    
    verify {
        someTopLevelFunction("some text")
    }