Java testing with Spock – Book Review

Earlier this year I was asked to evaluate a book concept about Spock framework. Taking into account that I am a big fan of this library I was extremely happy to see that measures were taken to create a book focused on this particular topic. Hopefully the book will boost even further the popularity of Spock. As always high interest of a product is the best motivation for its authors to continue its development. Few months later, I am given a possibility to read the early edition of this book and as well was asked for a review.


The book was classified by author as of intermediate level, so a knowledge of Java is a must. As Spock is a Groovy technology, a question rises then: is Groovy knowledge required or not? Definiately it would be of advantage, nevertheless you should be able to understand examples inside the book without knowing Groovy. Actually author of the book assumes that you come without any Groovy knowledge and dedicates one chapter to introduce you to different Groovy concepts and its syntax. Basic understanding of unit testing and testing in general would be beneficial as well, however author spends some time on explaing different concepts in testing (i.e. why we need tests, what kind of tests are useful, techniques like stubbing or mocking). Therefore, if you have not had much experience with testing before, this book might be a good starting point for you. If you had, it will help you to grasp how things are handled in Spock (in contrast to JUnit).

What will you learn?

  • why we need to test our applications / and what can happen if we dont,
  • about positive effects of using Spock and what makes it to step out among other testing frameworks,
  • how Spock is different from JUnit (a common industry standard),
  • why Spock can be considered as a tool for BDD,
  • what Groovy is
    • what its syntax is,
    • what we can achieve using Groovy that we cannot be done in pure Java (see my introduction to Groovy),
    • how it simplifies reading xml/json files,
    • how it simplifies creating objects with ObjectGraphBuilders and Expando;
  • how to create and run spock tests,
  • how to seamlessly introduce Spock to your existing applications,
  • how to split your tests into setups-stimulate-assert phases,
  • what mocks and stubs are
    • using stubs to return simple values,
    • using stubs to return dynamically changing values,
    • using stubs to throw exceptions
    • using mocks to verify interactions with system components,
    • how to write code that can be easily stubbed/mocked;
  • how to write self-documenting code (using @Subject, @Title, @Narrative),
  • how to create parametrized tests (in order to achieve DRY principle in your test code)
    • using data tables,
    • using data pipes,
    • using data custom generators;
  • how to add Spock to your project
    • using Maven,
    • using Gradle;
  • how to integrate Spock with IDE of your choice
    • IntelliJ,
    • Eclipse.

Final thoughts

Tests that we come up with using standard java frameworks can sometimes be real nightmares to maintain and understand. They just require too much of boilerplate code and introduce too much confusion to code. To be fair, I rarely enjoyed writing tests in the past and even less likely I would be truly satisfied with the final result. It has all changed with Spock, thanks to which writing tests is not anymore an annoying requirement, but started to be fun.

I can recommend this book to everyone how wants to learn Spock or even to those who has not yet given much of the attention to testing their code. I particularly find it very important that the book answers to the question why we need testing in the first place. Explaing what the perks of having automated tests are and what dangerous situations can be avoided as a consequence of having your application tested. For that reason I would consider this book as a a perfect gift for your fellow developers who are not very fond of testing their code (and believe me world is full of them).

The other thing I really appreciate about this book is that its author put a lot attention on readability of the code and reducing the bolierplate, giving various hints how to achieve both. I reckon that is a right direction to go as sometimes developers forget about leaving their test code clean and well organized. What is more, there are many features of Groovy and Spock which can greatly improve your code in both of these areas.

On the other hand, the version of the book I received has a number of typos and grammar mistakes but I expect these to be revised before the final release. As well I did not always agree with the author on the code he put in different sections of given/when/then sections but probably this will differ for every developer.

Even though the books has not been yet released you can start reading it right now through Manning Early Access Program (click here). Also if you are still not convinced about Spock you can have a look at my article on this framework to see if it fits your needs (click here).

See as well

Please join discussion