From a425d55a29e3ee6e4cfdee510e311375f4549db0 Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Mon, 13 Jan 2020 03:26:26 +0700 Subject: [PATCH] Improve callRealMethod() documentation (#1005) * Improve callRealMethod() documentation Closes #830. * A la carte mock needs an IDefaultResponse instance for 'defaultResponse', not a type See also https://stackoverflow.com/a/57305121/1082681 --- docs/interaction_based_testing.adoc | 7 ++++++- .../java/org/spockframework/mock/IMockInvocation.java | 9 +++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/interaction_based_testing.adoc b/docs/interaction_based_testing.adoc index 758c1f9175..5de092104a 100644 --- a/docs/interaction_based_testing.adoc +++ b/docs/interaction_based_testing.adoc @@ -805,6 +805,11 @@ Note that we don't have to pass the `message` argument along; this is taken care returns the real invocation's result, but in this example we opted to return our own result instead. If we had wanted to pass a different message to the real method, we could have used `callRealMethodWithArgs("changed message")`. +Please note that while semantically both `callRealMethod()` and `callRealMethodWithArgs(...)` only make sense with spies, +technically you can also call these methods on mock or stub objects, kind of turning them into (pseudo) spy objects +"through the backdoor". The only precondition is that the mocked/stubbed object actually has a real method implementation, +i.e. for interface mocks there must be a default method, for class mocks there must be a (non-abstract) original method. + [[PartialMocks]] === Partial Mocks @@ -963,7 +968,7 @@ can be passed as named arguments to the `Mock()` method. For example: [source,groovy] ---- -def person = Mock(name: "Fred", type: Person, defaultResponse: ZeroOrNullResponse, verified: false) +def person = Mock(name: "Fred", type: Person, defaultResponse: ZeroOrNullResponse.INSTANCE, verified: false) ---- Here, we create a mock whose default return values match those of a `Mock()`, but whose invocations aren't diff --git a/spock-core/src/main/java/org/spockframework/mock/IMockInvocation.java b/spock-core/src/main/java/org/spockframework/mock/IMockInvocation.java index 848148feb3..bca5cd865a 100755 --- a/spock-core/src/main/java/org/spockframework/mock/IMockInvocation.java +++ b/spock-core/src/main/java/org/spockframework/mock/IMockInvocation.java @@ -41,8 +41,9 @@ public interface IMockInvocation { /** * Delegates this method invocation to the real object underlying this mock object, - * including any method arguments. If this mock object has no underlying real object, a - * {@link CannotInvokeRealMethodException} is thrown. + * including any method arguments. + * If this mock object has no underlying real (non-interface) object or interface + * default method implementation, a {@link CannotInvokeRealMethodException} is thrown. * * @return the return value of the method to which this invocation was delegated */ @@ -51,8 +52,8 @@ public interface IMockInvocation { /** * Delegates this method invocation to the real object underlying this mock object, * replacing the original method arguments with the specified arguments. - * If this mock object has no underlying real object, a - * {@link CannotInvokeRealMethodException} is thrown. + * If this mock object has no underlying real (non-interface) object or interface + * default method implementation, a {@link CannotInvokeRealMethodException} is thrown. * * @return the return value of the method to which this invocation was delegated */