spockframework / spockframework/spock
GroovySpy not working when static methods invoked from within closure (1.0-groovy-2.3)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.6k
- Forks
- 483
- PR merge metrics
- No merged PRs in 30d
Description
Consider this class with some static methods:
class SomeClass {
public static String method1 () {
[1].each {
commonMethod()
}
}
public static String method2 () {
for (def i: [1]) {
commonMethod()
}
}
public static String commonMethod() {
}
}
method1 and method2 both call commonMethod but in different ways. method1 invokes commonMethod from a closure, while method2 invokes it from within a for loop.
Now consider the following spec:
class SomeClassSpec extends Specification {
// This fails
def "method1"() {
given:
def spy = GroovySpy(SomeClass, global:true)
when:
spy.method1()
then:
1 * SomeClass.commonMethod() >> "ASDF"
}
// This passes
def "method2"() {
given:
def spy = GroovySpy(SomeClass, global:true)
when:
spy.method2()
then:
1 * SomeClass.commonMethod() >> "ASDF"
}
}
I would expect both tests to pass, but the first one fails. In the first test, the real SomeClass.commonMethod is being invoked (not expected), while in the second test, it is not (expected).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided SomeClassSpec reproduction and compare the method1 closure path with the method2 loop path when using GroovySpy globally. Trace the entry points method1, method2, and commonMethod to determine why the closure call bypasses interception. Done means both examples prevent the real commonMethod from running and the regression test passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100