You are previewing Spock: Up and Running.
O'Reilly logo
Spock: Up and Running

Book Description

This definitive guide shows you how to use Spock’s expressive test capabilities to write concise and highly readable tests. Written by a senior software engineer at Netflix with more than 15 years of experience writing automated tests within the Java and Groovy ecosystems, this book takes you from Spock basics to advanced topics, using fully worked integration examples. Learn how initiate test-driven development in your team, using tests as living documentation.

Table of Contents

  1. Introduction
    1. Meet Spock
    2. A short background on Groovy
    3. Who is this book for?
    4. About this book
    5. Navigating This Book
    6. Online Resources
    7. Conventions Used in This Book
    8. Using Code Examples
    9. Safari® Books Online
    10. How to Contact Us
    11. Acknowledgments
  2. 1. The “up and running” part
    1. Installation
    2. Running with the JUnit runner
    3. Running in IDEs
    4. Running with Gradle
    5. Running with Maven
  3. 2. Specification basics
    1. The anatomy of a specification
      1. Why use quoted strings for feature method names?
    2. An introduction to Spock’s blocks
      1. Block taxonomy
      2. Basic block usage
    3. The specification lifecycle
      1. Fixture methods — setup and cleanup
    4. Block descriptions
      1. and: blocks
      2. A note on comprehensibility
    5. Summary
      1. Comparison with JUnit
  4. 3. Spock assertions
    1. The importance of good diagnostics
    2. The power assert
      1. Type information in power asserts
    3. Leveraging Groovy for effective assertions
      1. Using list comprehensions and list literals
      2. Asserting something about every item in a collection
      3. Expressive assertions with Groovy operators
    4. Expecting exceptions
      1. Interrogating the exception
    5. Grouping assertions on the same object
    6. Summary
  5. 4. Managing resources
    1. How (not) to approach persistence testing
    2. Testing a persistence layer
      1. Managing resources with the Spock lifecycle
    3. Specifications and inheritance
      1. Execution order of lifecycle methods in an inheritance hierarchy
    4. Summary
  6. 5. Interaction testing
    1. Asserting that a method is called
    2. Specifying invocation cardinality
    3. Declaring the return value from a mocked method
    4. Parameter predicates
    5. Disallowing unexpected calls or “strict mocking”
    6. Looser matching with wildcard parameters
      1. Wildcard shortcuts
    7. Defining behavior for mocked methods
    8. Removing invocation constraints with stubs
      1. When should I use a mock and when should I use a stub?
      2. Mixing mocks and stubs
    9. Summary
  7. 6. Parameterized specifications
    1. Paramaterization in JUnit
    2. Spock’s where: block
    3. Separating iteration reporting using @Unroll
      1. Adding @Unroll descriptions
      2. @Unroll tokens in feature method names
      3. Class level @Unroll annotations
    4. Multiple data pipes
    5. Data tables
    6. Fields and methods as data providers
    7. Derived values
      1. Clarifying @Unroll expressions
    8. Summary
  8. 7. Advanced where: blocks
    1. A primary key verifier
    2. A static site link checker
      1. JBake configuration
      2. A data pipe driven by a file
      3. A filtered data pipe
      4. Relative file paths
    3. Summary
  9. 8. Asynchronous testing
    1. Blocking constructs
      1. BlockingVariable
      2. BlockingVariables
    2. Testing non-blocking APIs with callbacks
      1. Using BlockingVariable in a callback
      2. Using a reference to BlockingVariable.set as a callback
      3. Awaiting multiple values with BlockingVariables
    3. Polling for eventual state
      1. Using PollingConditions as a delegate
    4. Asynchronous interaction testing
      1. Asynchronous parameter capture
      2. Extending BlockingVariable
  10. 9. Extending Spock
    1. The message timeline
    2. Hamcrest matchers
      1. Hamcrest support in Spock
    3. JUnit rules
      1. Reusable data cleanup
      2. Simplifying the rule with ExternalResource
      3. Making the cleanup logic generic
    4. Spock Extensions
      1. Data cleanup as a Spock extension
      2. Further steps
    5. Global extensions
      1. Taking it Further
    6. Summary
  11. 10. TDD: The Diamond Kata
    1. The Diamond Kata
    2. The implementation
      1. First step: The pathological case
      2. The simplest valid diamond
      3. Enforcing invariants
      4. Matrix content
      5. Symmetry
      6. Eliminating the assertion loop
      7. Adding padding to the rows
      8. Refinement
    3. Summary
  12. 11. Testing REST APIs
    1. Defining the application URL
    2. A first specification
    3. Web application lifecycle
    4. Creating data for end-to-end tests
      1. Bleeding data between tests
      2. Creating test data on demand
      3. Cleaning up test data
    5. Requests with data
      1. Fixture methods
    6. Testing for HTTP errors
    7. Verifying response data
      1. Typed response entities
    8. Multiple HTTP requests in a feature method
    9. Back-doors or “pure” API testing?
    10. Handling redirect responses
      1. Verifying redirect details
    11. REST API security
      1. Authentication vs authorization
    12. Summary
  13. 12. End-to-end web testing
    1. Geb
    2. Getting started with Geb
    3. A first front-end test
    4. Introducing page models
      1. Setting the base URL
      2. Geb’s automatic delegation
    5. Authentication
      1. Encapsulating interactions with methods
    6. Modules
    7. Parameterized pages
    8. Interacting with forms
      1. Accessing form elements by name
      2. Encapsulating interactions with methods
    9. Form validation
  14. 13. Test driving an RxJava application
    1. Tests, behaviors and properties
    2. The skeleton implementation
    3. Test driving the implementation
      1. Defining the behavior
      2. A first test
    4. Polling at a regular interval
      1. No sleep `til the test goes green
      2. Testing subscription to the stream
      3. Un-chunking the message stream
      4. Requesting messages since the last received
      5. Recovering from polling errors
      6. Recovering from downstream errors
    5. A final implementation