mockObject

fun <T : Any> mockObject(instance: T): MockedObject<T>

Creates a thread-local mock for an object or companion object singleton.

NOTE: This returns a MockedObject which must be closed after test execution to prevent mocking state from leaking to other test cases. You can do this with a .use {} block or by calling MockedObject.close manually.

Example usage:

mockObject(MyObject).use {
whenever(MyObject.foo()).thenReturn("hello")
}

or with a companion object with method bar():

mockObject(MyClass.Companion).use {
whenever(MyClass.bar()).thenReturn("hello")
}

fun <T : Any> mockObject(instance: T, stubbing: KStubbing<T>.(T) -> Unit): MockedObject<T>

Creates a thread-local mock for an object or companion object singleton, allowing for immediate stubbing.

NOTE: This returns a MockedObject which must be closed after test execution to prevent mocking state from leaking to other test cases. You can do this with a .use {} block or by calling MockedObject.close manually.

Example usage:

mockObject(MyObject) {
on { foo() } doReturn "hello"
}.use { ... }

or with a companion object with method bar():

mockObject(MyClass.Companion) {
on { bar() } doReturn "hello"
}.use { ... }