spy Object
Creates a thread-local spy for an object or companion object singleton.
Unlike mockObject, which replaces all methods with stubs, spyObject delegates to the real implementation by default. Only explicitly stubbed methods are overridden.
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:
spyObject(MyObject).use {
whenever(MyObject.foo()).thenReturn("hello")
// unstubbed methods call through to real implementations
}Creates a thread-local spy for an object or companion object singleton, allowing for immediate stubbing.
Unlike mockObject, which replaces all methods with stubs, spyObject delegates to the real implementation by default. Only explicitly stubbed methods are overridden.
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:
spyObject(MyObject) {
on { foo() } doReturn "hello"
}.use { ... }