Chapter 3. A tour of Spock functionality

 

This chapter covers

  • Understanding the given-when-then Spock syntax
  • Testing datasets with data-driven tests
  • Introducing mocks/stubs with Spock
  • Examining mock behavior

With the Groovy basics out of the way, you’re now ready to focus on Spock syntax and see how it combines several aspects of unit testing in a single and cohesive package.

Different applications come with different testing needs, and it’s hard to predict what parts of Spock will be more useful to you beforehand. This chapter covers a bit of all major Spock capabilities to give you a bird’s-eye view of how Spock works. I won’t focus on all the details yet because these are explained in the coming chapters.

The purpose of this chapter is to act as a central hub for the whole book. You can read this chapter and then, according to your needs, decide which of the coming chapters is of special interest to you. If, for example, in your current application you have tests with lots of test data that spans multiple input variables, you can skip straight to the chapter that deals with data-driven tests (chapter 5).

The following sections briefly touch on these three aspects of Spock:

  • Core testing of Java code (more details in chapter 4)
  • Parameterized tests (more details in chapter 5)
  • Isolation of the class under test (more details in chapter 6)

3.1. Introducing the behavior-testing paradigm

3.2. Handling tests with multiple input sets

3.3. Isolating the class under test

3.4. Summary