Skip to content

Commit

Permalink
Improve callRealMethod() documentation (spockframework#1005)
Browse files Browse the repository at this point in the history
* Improve callRealMethod() documentation

Closes spockframework#830.

* A la carte mock needs an IDefaultResponse instance for 'defaultResponse', not a type

See also https://stackoverflow.com/a/57305121/1082681
  • Loading branch information
kriegaex authored and leonard84 committed Jan 12, 2020
1 parent e14cdb6 commit a425d55
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 6 additions & 1 deletion docs/interaction_based_testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down

0 comments on commit a425d55

Please sign in to comment.