mock Object
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")
}Content copied to clipboard
or with a companion object with method bar():
mockObject(MyClass.Companion).use {
whenever(MyClass.bar()).thenReturn("hello")
}Content copied to clipboard
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 { ... }Content copied to clipboard
or with a companion object with method bar():
mockObject(MyClass.Companion) {
on { bar() } doReturn "hello"
}.use { ... }Content copied to clipboard