It's very simple to create a CreateUnitSpec or CreateIntegrationSpec script with a template specification. Hooking in to the other types of artifact creation turned out to be fiddlier. Each create-* command calls a Closure called createUnitTest. Reassigning that Closure should be the solution. The trick is in figuring out where that can be done.
Any time one of its Gant targets is invoked the Grails build system fires an event. You can respond to those events by declaring a closure called
event<target name>Start
in scripts/_Events.groovy
. The only Gant target directly invoked when an artifact is created is called 'default'. It is possible to intercept that although that means the event handler will be invoked any time any Gant target called 'default' runs. For this purpose that's no problem since we're just overriding a closure in the build binding.The other factor is that the superclass for the unit test is specified by the individual create-* scripts (or defaulted to GrailsUnitTestCase). Rather than having to override those scripts as well, I've just mapped the non-standard unit test superclasses to the Spock equivalents.
Here's the code for your
_Events.groovy
script:The template specification should be placed in
src/templates/artifacts/Spec.groovy
and is simply:It goes without saying that this is a slightly hairy and it would be great if Grails provided a proper hook for overriding the test generation. I can live with some fun-size evil in _Events.groovy for the sake of the convenience of getting template specs for all my artifacts, though.
9 comments:
This has been a long-standing feature request for spock-grails. In other words, it's a great opportunity to become an official Spock contributor/committer. :-)
Cool, I'll send Luke a patch
I've also added a hook that generates integration specs:
By adding the following below the createUnitTest closure it should all work:
createIntegrationTest = { Map args = [:] ->
createArtifact name: args["name"], suffix: "${args['suffix']}Spec", type: "Spec", path: "test/integration", superClass: "IntegrationSpec"
}
It would be nice if this was included into the grails spock plugin by default ;-)
These two enhancements are currently included in the Spock plugin by default.
I am getting this error:
Groovy:unable to resolve class
UnitSpec
I created a unit test for my DomainClass Employees and it produced
import spock.lang.*
import grails.plugin.spock.*
class EmployeesSpec extends UnitSpec {
def "feature method"() {
}
}
I changed to UnitTest to Specification, and the class looks good without errors.
Did I miss something?
Thanks for posting this.write-my-essays.org
I wish it was able to be translated, but for some reason Google toolbar isn't working. I copy pasted it into another application and read the post.
replica bags india replica bags paypal replica bags in dubai
Experience sacred synergy with Apna Showroom’s 7-in-1 devotional combo. This holistic bundle offers a Brass Kalash with Gangajal, Feng Shui tortoise, powerful crystal, Shaligram Shivling, Rudraksha mala, Five Rose Pin, and confident fishnet stockings. ApnaShowroom Men's Women's Metal Hair Bands Black Combo set of 5 hairband Unisex
Apna Showroom Dressing Hair Comb for Men, Kids, Girls, Ladies And Women
Apna Showroom Plastic Hair Lice Combs for Women and Girls (Multicolour) with desent look
APNA SHOWROOM Attractive Rings for Women 10 PCS Combo Ring Set Crafted Rings for Women and Girls
Apna Showroom Gangajal Brass Kalash | Filled from 'Brahma Kund' Har Ki Pauri, Haridwar | Authentic Ganga Jal Lota | 250 ML
Designed for spiritually stylish women who ground their energy and glow with intention. Great for rituals, décor, and meaningful gifting. Shop this perfect fusion of spiritual symbolism and sensual strength today—it’s where divinity meets expression.
Post a Comment