1.5. A first look at Spock in action

The following examples should whet your appetite, so don’t stress over the strange syntax or any unknown keywords. I cover Spock syntax throughout the rest of the book.

1.5.1. A simple test with JUnit

When introducing a new library/language/framework, everybody expects a “hello world” example. This section shows what Spock looks like in a minimal, but fully functional example.

The following listing presents the Java class you’ll test. For comparison, a possible JUnit test is first shown, as JUnit is the de facto testing framework for Java, still undisputed after more than a decade.

Listing 1.2. Java class under test and JUnit test

You introduce two test methods, one that tests the core functionality of your ...

Get Java Testing with Spock now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.