Java Testing with Spock: A good (and sometimes Groovy) guide to using this powerful testing framework – #programming #java

Java Testing with Spock

Konstantinos Kapelonis

Manning, paperback

 

Spock, the author states, is “a comprehensive testing framework for Java (and Groovy) code that can help you automate the boring, repetitive, and manual process of testing a software application. Spock is comprehensive because it’s a union of existing testing libraries”—specifically JUnit, Mockito and JBehave. It also is influenced by several others.

What is Spock’s main advantage in test scenarios? “When things go wrong,” Konstantinos Kapelonis notes, “Spock gives as much detail as possible on the inner workings of the code at the time of the failure.”

Spock is written in Groovy, and just mentioning that language, as well as the Gradle build tool, may give a little heartburn to hardcore Java developers who don’t want to learn them. But others find Groovy refreshingly efficient and Gradle easy to use. In any case, using Groovy (and Gradle) with this book is “optional,” the author emphasizes. As noted in Appendix A, “It’s perfectly possible to use Spock in your Java project without installing Groovy itself.”

To emphasize that point, Kapelonis shows how to use Spock with the Maven build tool first, before he delves into how to use Spock with the Gradle build tool.

The book is divided into three major parts: (1) Foundations and brief tour of Spock; (2) Structuring Spock tests; and (3) Spock in the Enterprise.

Two appendices deal with installing and using Spock, plus getting your IDE set up, and using the book’s example files.

Java Testing with Spock is a comprehensive guide to learning how to do Java (and Groovy) testing with Spock, and it is generally well written and adequately illustrated.

I chose to try the Groovy-Gradle approach, with Eclipse as my IDE. And I did run into some awkward moments trying to get Eclipse Mars.2 to play correctly. The Groovy-Gradle plug-in from the Eclipse Marketplace was for earlier versions of Eclipse, and so was the Spock plug-in. After some tinkering and reconfiguring, I was able to get things working together and do some Java and Groovy tests. To be fair, I was doing this on a kludged-together Windows 10 machine that definitely is no development powerhouse. And I did not have time to try out the Maven approach, but I have used Maven in the past, and the author’s instructions and examples for Maven look solid.

Java Testing with Spock is a good, helpful how-to book for anyone who wants to know more about putting the Spock testing framework to good use at all levels of Java development.

Si Dunn

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s