O'Reilly logo
live online training icon Live Online training

Unit testing with the Spock framework

Writing and executing tests for Java and related languages

Ken Kousen

Testing is fundamental to writing high-quality code. The Spock testing framework emerged from the Groovy community as a powerful way to write unit and integration tests. It combines the simplicity and intuitive power of Groovy with a sophisticated mocking framework that makes writing and executing unit tests simple. Expert Ken Kousen walks you through the major features of Spock. You'll get hands-on experience performing basic tests and learn how to test persistence layers and RESTful web services, create and verify mock objects, and more.

What you'll learn-and how you can apply it

By the end of this live online course, you’ll understand:

  • How Spock tests leverage existing JUnit infrastructure
  • Spock annotations and where they apply
  • Spock mocks and stubs and how to take advantage of them

And you’ll be able to:

  • Set up test fixtures
  • Run testing methods before and after tests and test classes
  • Mock classes and interfaces, set expectations, and validate them

This training course is for you because...

  • You're a Java developer who normally writes unit tests using a compatible library like JUnit.
  • You're interested in test-driven development and would like to learn an easy way to do it.
  • You intend to test code written in any JVM-based language (Java, Groovy, Kotlin, etc.).

Prerequisites

Recommended preparation:

Modern Java Recipes (book)

Recommended follow-up:

Introduction to Spock (video)

The Spock Testing Framework (video)

Spock: Up and Running (book)

About your instructor

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Installing and configuring Spock (25 minutes)

  • Lecture: Using a build tool; taking advantage of IDE integration
  • Hands-on exercise: Using Spock tests in a build tool and IDE

Writing Spock tests (50 minutes)

  • Lecture: Fixture methods; feature methods; basic Spock blocks
  • Hands-on exercise: Basic tests for Java classes

Break (10 minutes)

Spock in detail (45 minutes)

  • Lecture: Using "when/then" pairs, "expect," and more; pre- and postprocessing methods; the "old" method; testing for exceptions with "thrown" and "notThrown"
  • Hands-on exercise: Testing for exceptions

Break (10 minutes)

Data-driven and database-driven specs (40 minutes)

  • Lecture: Testing DAO layers; the "where" block; using the @Unroll annotation
  • Hands-on exercise: Testing DAO layers and RESTful web services

Break (10 minutes)

Spock mocks (30 minutes)

  • Lecture: Differences between mocks and stubs; setting expectations; working with multiplicity and ordering; the underscore wildcard and patterns
  • Hands-on exercise: Mocking interfaces and classes

Miscellaneous features and extensions (20 minutes)

  • Lecture: Setting time-out periods for tests; conditional execution; ignoring tests under certain circumstances; working with the Spring TestContext framework; resources for future study