You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Bryan Stopp (JIRA)" <ji...@apache.org> on 2019/07/11 15:11:00 UTC
[jira] [Updated] (GROOVY-9188) Partial mocking static method calls
with Ignore & Demand don't work as expected.
[ https://issues.apache.org/jira/browse/GROOVY-9188?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Bryan Stopp updated GROOVY-9188:
--------------------------------
Description:
When defining a mockFor on a class with static methods, if you _ignore_ a method which invokes a _demand_ mocked method. The _demand_ is not intercepted.
Here is an example modified from the MockFor documentation:
import groovy.mock.interceptor.MockFor
class Person {
static def ignoreMe(foo) { foo }
static def ignoreMeToo() { ignoreMe('foo') }
static def ignoreMeThree() { ignoreMe('foo') }
}
def mock = new MockFor(Person)
mock.ignore('ignoreMeToo')
mock.ignore('ignoreMeThree')
mock.demand.ignoreMe { s -> 'baz' }
mock.use {
assert Person.ignoreMe('foo') == 'baz'
assert Person.ignoreMeToo() == 'baz'
assert Person.ignoreMeThree() == 'baz'
}
Running this will result in a failure as *Person.ignoreMeToo()* returns `foo` instead of `baz`.
Use Case: Mocking nested static method calls. I want to test a particular method. I know/expect that method to invoke another static method on the same class, but I want to mock that one, and return an expected value.
was:
When defining a mockFor on a class with static methods, if you _ignore_ a method which invokes a _demand_ mocked method. The _demand_ is not intercepted.
Here is an example modified from the MockFor documentation:
```
import groovy.mock.interceptor.MockFor
class Person {
static def ignoreMe(foo) { foo }
static def ignoreMeToo() { ignoreMe('foo') }
static def ignoreMeThree() { ignoreMe('foo') }
}
def mock = new MockFor(Person)
mock.ignore('ignoreMeToo')
mock.ignore('ignoreMeThree')
mock.demand.ignoreMe { s -> 'baz' }
mock.use {
assert Person.ignoreMe('foo') == 'baz'
assert Person.ignoreMeToo() == 'baz'
assert Person.ignoreMeThree() == 'baz'
}
```
Running this will result in a failure as *Person.ignoreMeToo()* returns `foo` instead of `baz`.
Use Case: Mocking nested static method calls. I want to test a particular method. I know/expect that method to invoke another static method on the same class, but I want to mock that one, and return an expected value.
> Partial mocking static method calls with Ignore & Demand don't work as expected.
> --------------------------------------------------------------------------------
>
> Key: GROOVY-9188
> URL: https://issues.apache.org/jira/browse/GROOVY-9188
> Project: Groovy
> Issue Type: Bug
> Components: mocks and stubs
> Affects Versions: 2.5.7
> Reporter: Bryan Stopp
> Priority: Major
>
> When defining a mockFor on a class with static methods, if you _ignore_ a method which invokes a _demand_ mocked method. The _demand_ is not intercepted.
> Here is an example modified from the MockFor documentation:
>
> import groovy.mock.interceptor.MockFor
> class Person {
> static def ignoreMe(foo) { foo }
> static def ignoreMeToo() { ignoreMe('foo') }
> static def ignoreMeThree() { ignoreMe('foo') }
> }
> def mock = new MockFor(Person)
> mock.ignore('ignoreMeToo')
> mock.ignore('ignoreMeThree')
> mock.demand.ignoreMe { s -> 'baz' }
> mock.use {
> assert Person.ignoreMe('foo') == 'baz'
> assert Person.ignoreMeToo() == 'baz'
> assert Person.ignoreMeThree() == 'baz'
> }
>
> Running this will result in a failure as *Person.ignoreMeToo()* returns `foo` instead of `baz`.
> Use Case: Mocking nested static method calls. I want to test a particular method. I know/expect that method to invoke another static method on the same class, but I want to mock that one, and return an expected value.
--
This message was sent by Atlassian JIRA
(v7.6.14#76016)