mock Extension Fun
Creates a thread-local mock for the static methods of the class that contains this top-level extension function.
Top-level Kotlin extension functions compile to static methods in a *Kt class. This helper simplifies creating a MockedStatic for them.
Usage:
fun String.isHello(): Boolean = this == "Hello"
mockExtensionFun(String::isHello).use {
whenever("test".isHello()).thenReturn(true)
println("test".isHello()) // "true"
}Content copied to clipboard
When using matchers, all arguments including the receiver must use matchers:
fun String.hasPrefix(prefix: String): Boolean = this.startsWith(prefix)
mockExtensionFun(String::hasPrefix).use {
whenever(any<String>().hasPrefix(eq("foo"))).thenReturn(true)
println("bar".hasPrefix("foo")) // "true"
}Content copied to clipboard
Note: member extension functions (extension functions declared inside a class) do not need this helper. They can be mocked by creating a regular mock of the containing class.
Parameters
function
a reference to the top-level extension function to mock.
See also
Mockito. mockStatic