Mock top-level and extension functions

Top-level functions

Kotlin lets you write functions that don’t belong to any class or object, called top-level functions. These calls are translated to static methods in Java, and a special Java class is generated to hold the functions. These top-level functions can be mocked using mockkStatic, but you need to figure out the class name of this generated Java class.

Finding the class name

TODO

Extension functions

Depending on where an extension function is located, it may correspond to a top-level function or a class member. If placed inside a class, the extension function is attached to that class and can be mocked using the with method.

  1. // TODO val mock = ...
  2. with (mock) {
  3. every { any<Type>().extensionFunction() } returns "result"
  4. }

If the extension function is inside an object, the code is similar but the object needs to be mocked first.

When the extension function is in the top-level of a file, it can be mocked like other top-level functions using mockkStatic.