Chapter 9
[ 245 ]
You can explore this a little yourself by launching a Spock test in the debugger of
your favorite IDE. Place a breakpoint on a line within the first feature method of
your spec. When the debugger hits the breakpoint, you will notice by looking at
the call stack that you are not in the feature method you defined in the test, but in a
method called $spock_feature_0_0. The Spock AST transformation has created this
new method for you.
Try stepping through the lines of code in a block. You will notice that the debugger
stays in sync. This is because Spock clones the original source location from the
original AST objects into the newly created ones. The ASTNode class has a method
specifically for doing this: ASTNode.setSourcePosition(ASTNode node). This is
an important feature to use if you are going to build an AST transformation-based
DSL, which you hope to behave well in a debugger.
Spock does a lot more sophisticated stuff than this under the covers. In fact, it is one
of the first DSLs produced in Groovy that exploited the AST transformations feature
when it was added to Groovy. I encourage you to explore the Spock sources. They
can be found on GitHub at https://github.com/spockframework/spock.
Summary
In this chapter, we looked at two existing Groovy DSLs that are in current use and
are free to download. GORM implements a full persistence layer over Hibernate that
layers over standard Groovy classes. GORM allows us to decorate a regular POGO
with settings for applying the most common associations and relationships that we
can expect in our object models.
Much of what GORM provides in terms of querying via dynamic finders requires a
Groovy-knowledgeable developer to appreciate and use them. However, the basic
object modeling semantics provided through the belongsTo, hasMany, and other
persistence settings could be used quite readily by a data-modeling architect who has
little or no knowledge of the Groovy language. The domain classes can be viewed
as an independent model specification language, which has the advantage of being
immediately usable by Groovy developers responsible for other parts of the system.
Spock brings BDD style specification-based testing to the Java/Groovy platform.
Each provides a means to write specifications by using given/when/then style
semantics that are easier to interpret than regular xUnit style testing frameworks.
The pseudo-English style syntax of this DSL should mean that the specifications
can be understood by business stakeholders even if they still need to be coded by a
Groovy-proficient developer.