Chapter 2. Groovy knowledge for Spock testing

 

This chapter covers

  • Understanding the connection between Java and Groovy
  • Learning Groovy conventions
  • Comparing JUnit asserts and Groovy power asserts
  • Using Groovy utilities for common testing needs
  • Obtaining test data with Groovy

Learning a new programming language is usually a daunting task. You must study a new syntax, new concepts, and new libraries all at once to be productive. If you’ve spent too many years with a single language, several concepts are so entrenched that having to “unlearn” them poses a big barrier to any alternative (even if it’s objectively better). With Groovy, this isn’t the case because Groovy is a cousin language to Java. Much of your current knowledge can be reused and extended instead of thrown away.

This chapter gives you a crash course in the essentials of Groovy. It’s important to know your way around Groovy code before writing Spock tests. I’ve seen several Java developers who jump into Spock, but write the same JUnit-like tests as they did before.

2.1. What you need to know about Groovy

2.2. Groovy Power assert as a replacement for JUnit asserts

2.3. Groovy features useful to Spock tests

2.4. Reading a test dataset from an external source

2.5. Advanced Groovy features useful to testing

2.6. Summary