You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Ryan Tandy (Jira)" <ji...@apache.org> on 2021/02/08 22:28:00 UTC
[jira] [Updated] (GROOVY-9932) MockFor/StubFor makes mocked
internal method return null
[ https://issues.apache.org/jira/browse/GROOVY-9932?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Ryan Tandy updated GROOVY-9932:
-------------------------------
Description:
{code:groovy}
import groovy.mock.interceptor.StubFor
class Helper {
String string
Helper() { string = method('arg') }
String method(String arg) { return 'test' }
}
StubFor stub = new StubFor(Helper)
stub.demand.method { String arg -> return 'stub' }
stub.ignore('getString')
stub.use {
Helper helper = new Helper()
assert helper.string == 'stub'
}
{code}
{{method()}} is a public method, thus I expect it to respect mocking, even for internal calls. Let me know if this expectation is wrong.
With Groovy 2.4.21, the test works as expected. With Groovy 3.0.7, it fails:
{noformat}
Assertion failed:
assert helper.string == 'stub'
| | |
| null false
Helper@5c00384f
at test$_run_closure2.doCall(test.groovy:17)
at test$_run_closure2.doCall(test.groovy)
at test.run(test.groovy:15)
{noformat}
Instead of either the original or mocked return value, we got {{null}} instead.
It looks like it works for {{call()}}, but not for {{callCurrent()}}. I think the issue is that MockProxyMetaClass overrides one signature of {{invokeMethod()}}, but not the one used by {{callCurrent()}}. Might [34ad466ba6a|https://github.com/apache/groovy/commit/34ad466ba6a] be a relevant change?
was:
{code:groovy}
import groovy.mock.interceptor.StubFor
class Helper {
String string
Helper() { string = method() }
String method(String arg) { return 'test' }
}
StubFor stub = new StubFor(Helper)
stub.demand.method { String arg -> return 'stub' }
stub.ignore('getString')
stub.use {
Helper helper = new Helper()
assert helper.string == 'stub'
}
{code}
{{method()}} is a public method, thus I expect it to respect mocking, even for internal calls. Let me know if this expectation is wrong.
With Groovy 2.4.21, the test works as expected. With Groovy 3.0.7, it fails:
{noformat}
Assertion failed:
assert helper.string == 'stub'
| | |
| null false
Helper@5c00384f
at test$_run_closure2.doCall(test.groovy:17)
at test$_run_closure2.doCall(test.groovy)
at test.run(test.groovy:15)
{noformat}
Instead of either the original or mocked return value, we got {{null}} instead.
It looks like it works for {{call()}}, but not for {{callCurrent()}}. I think the issue is that MockProxyMetaClass overrides one signature of {{invokeMethod()}}, but not the one used by {{callCurrent()}}. Might [34ad466ba6a|https://github.com/apache/groovy/commit/34ad466ba6a] be a relevant change?
> MockFor/StubFor makes mocked internal method return null
> --------------------------------------------------------
>
> Key: GROOVY-9932
> URL: https://issues.apache.org/jira/browse/GROOVY-9932
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 3.0.7
> Reporter: Ryan Tandy
> Priority: Minor
>
> {code:groovy}
> import groovy.mock.interceptor.StubFor
> class Helper {
> String string
> Helper() { string = method('arg') }
> String method(String arg) { return 'test' }
> }
> StubFor stub = new StubFor(Helper)
> stub.demand.method { String arg -> return 'stub' }
> stub.ignore('getString')
> stub.use {
> Helper helper = new Helper()
> assert helper.string == 'stub'
> }
> {code}
> {{method()}} is a public method, thus I expect it to respect mocking, even for internal calls. Let me know if this expectation is wrong.
> With Groovy 2.4.21, the test works as expected. With Groovy 3.0.7, it fails:
> {noformat}
> Assertion failed:
> assert helper.string == 'stub'
> | | |
> | null false
> Helper@5c00384f
> at test$_run_closure2.doCall(test.groovy:17)
> at test$_run_closure2.doCall(test.groovy)
> at test.run(test.groovy:15)
> {noformat}
> Instead of either the original or mocked return value, we got {{null}} instead.
> It looks like it works for {{call()}}, but not for {{callCurrent()}}. I think the issue is that MockProxyMetaClass overrides one signature of {{invokeMethod()}}, but not the one used by {{callCurrent()}}. Might [34ad466ba6a|https://github.com/apache/groovy/commit/34ad466ba6a] be a relevant change?
--
This message was sent by Atlassian Jira
(v8.3.4#803005)