Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Essential Groovy DSLs


[ 42 ]

Dependencies


The final section in our preceding build script declares the dependencies we need
to resolve. Gradle is a Groovy-based tool, so it already ships with Groovy library.
However, in order to declare what version of Groovy we want used by our own
builds, we need to declare that as a dependency. The previous dependencies
section declares a compile time dependency on the latest Groovy version available
at time of publication:


dependencies {
compile "org.codehaus.groovy:groovy-all:2.4.4
testCompile "org.spockframework:spock-core:0.7-groovy-2.0"
}

Dependencies can be declared for compile and runtime phases of the build. We can
declare a dependency that is just to be used at the test compilation phase with a
testCompile configuration. In the previous example, we configured a dependency
for the Spock test framework, which is the second of our essential Groovy DSLs.


Spock tests


The Spock Framework is a behavior-driven development (BDD) tool built on top
of JUnit. Spock uses the features of the Groovy language to add BDD style syntax
to JUnit tests. Spock allows us to write our test specifications as "given, when, then"
style expressions, but under the covers, they still run as JUnit tests and can still be
run by the JUnit runner. This means that Spock tests are compatible with most build
tools, IDEs, and continuous integration servers.


Given, when, then


The main characteristic of BDD is the concept of expressing tests in plain English
sentences. User stories should be accompanied with clear descriptive acceptance
criteria, which describe expected behavior in the system under test.


In support of this concept, an accepted dialect has evolved to describe acceptance
tests. The general form follows the pattern: given (some initial context), when
(some event or stimulus occurs), then (a certain outcome is expected).


A simpler version when no stimulus is involved is given (some initial context) and
expect (certain condition to be true). The Spock DSL allows us to specify tests using
these familiar terms.


http://www.ebook3000.com
Free download pdf