Appendix A. Groovy Primer

This appendix is a crash course in the features of the Groovy programming language that you need to know in order to write Spock specifications effectively. It is not meant to be an exhaustive language reference. Partly because there are some language features—metaprogramming, for example—that are very complex but of limited use in understanding the examples in this book, but mostly because there is already a plethora of books on Groovy written by much smarter people than me.


Groovy was started in 2003 by James Strachan. Version 1.0 was released on 2007, and version 2.0 in 2012. In 2015, Groovy became a project of the Apache foundation. As of this writing, the current version is 2.4.8.

The language is syntactically derived from Java. In fact most pre–Java 8 Java code is also valid Groovy code. However, Groovy also takes a lot of inspiration from Ruby and Smalltalk.

From its inception, Groovy was a dynamic language. Method calls and property references are dispatched at runtime and can be intercepted by a type’s “metaclass” in order to extend the functionality of a class.

Groovy 2.0 added optional compile-time type checking and static compilation.

Syntactic and Semantic Differences to Java

There are some obvious syntactic differences that are noticeable the first time you look at Groovy code.


Groovy does not require a semicolon at the end of each statement. In fact, it is considered nonidiomatic to use semicolons. The only time semicolons ...

Get Spock: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.