spockframework / spockframework/spock
Allow creation of Spock mocks in traits
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.6k
- Forks
- 483
- PR merge metrics
- No merged PRs in 30d
Description
I tried to refactor some common setup code into a trait, but it appears that (as of 1.0/2.4) calling Mock() inside a trait is not supported:
Caused by: org.spockframework.runtime.InvalidSpecException: Mock objects can only be created inside a spec, and only during the lifetime of a feature (iteration)
at spock.lang.MockingApi.invalidMockCreation(MockingApi.java:1233)
at spock.lang.MockingApi.Mock(MockingApi.java:142)
at spock.lang.MockingApi$Mock.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at my.trait.MyTrait$Trait$Helper.$init$(MocksStripe.groovy:22)
I get this error regardless of whether the Mock() call is in a field initializer or in the setup() method, though it's thrown during spec initialization in the former case and on execution in the latter.
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 at spock.lang.MockingApi.invalidMockCreation and the Mock entry point cited in MockingApi.java, then reproduce the report using a trait field initializer and setup() call like MocksStripe.groovy:22. Trace how trait initialization and feature lifetimes are detected; done means valid Mock() calls in both trait contexts work without breaking the existing spec-only lifecycle rules.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy, java
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100