O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. O’Reilly Safari
    10. How to Contact Us
    11. Acknowledgments
  2. 1. The “up and running” part
    1. Installation
    2. Running with the JUnit runner
    3. Running with Gradle
      1. When Groovy is used in the project
      2. Synchronizing Groovy versions between main and test classpaths
    4. Running with Maven
  3. I. Spock 101
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. II. Advanced Spock
  10. 7. Idiomatic Spock
    1. Organizing tests
      1. The “test-per-method” anti-pattern
    2. Well-factored specifications
      1. Sharing helper methods
      2. Sharing methods with import static
      3. Sharing methods with Groovy traits
      4. Sharing methods with delegation
      5. Sharing methods with Groovy traits
      6. Helper methods and assertions
      7. Helper methods and mock interations
      8. Comparing “before” and “after” values
    3. The grammar of blocks
      1. Separation of logic and data with where: blocks
      2. when/then or given/expect // TODO: better heading
      3. Separating preconditions
      4. Separating preconditions from actions
    4. Method parameter capture with mocks
      1. Using a Closure as a parameter predicate
      2. Using Hamcrest for parameter predicates
      3. Parameter capture using a mock responder
    5. @Stepwise specifications
      1. How to use @Stepwise
      2. Disadvantages of @Stepwise
    6. Conditional specifications
      1. Marking pending features
      2. Selectively executing tests
    7. Automatically cleaning up resources
    8. Documenting specifications
  11. 8. 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
  12. 9. 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
  13. 10. 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
  14. 11. 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
  15. III. Integrating Spock
  16. 12. Spock and Spring
    1. What do we mean by “integration tests”?
      1. Container tests
    2. Spring compatibility
      1. @SpringBootTest
    3. Testing annotation-driven transactions
      1. Tracking who is mentioned in a message
      2. Non Spring-dependent tests
      3. Testing a transaction rollback
      4. Setting up the Spring application
      5. A first integration test
      6. Convenience and cost
      7. Unexpected side-effects
      8. Forcing Spring to recreate the application context
      9. A better way to force errors
    4. Mock and stub beans
      1. Mention events
      2. Mock beans before Spock 1.1
      3. Mock bean delegates
      4. Detached mocks in the Spring context
      5. Spring Auto-configuration and test configuration classes
      6. Mock beans in XML configuration
      7. Replacing application beans with mocks
    5. Declarative test data
      1. Using the @Sql annotation for data fixtures
      2. Global fixture data
      3. Cleaning up test data Spring style
    6. Tests in transactions
      1. Propagated transactions
    7. Summary
  17. 13. 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
  18. 14. 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
  19. 15. Testing JavaScript with Spock
    1. Why test JavaScript from the JVM?
    2. Setting up Nashorn for tests
    3. Calling JavaScript functions
      1. JavaScript functions as objects
      2. Specifying this in a JavaScript function
    4. Testing an isomorphic view component
      1. An Invocable script engine as a delegate
    5. Passing mocks to JavaScript
    6. Rendering a view with the Handlebars library
      1. Compiling Handlebars templates
      2. Testing template rendering
      3. Handling properties that don’t convert to JavaScript
      4. Registering Handlebars helper functions
      5. Composing templates with Handlebars partials
      6. Passing iterable values to JavaScript
    7. Summary
  20. 16. 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
  21. A. Groovy Primer
    1. History
    2. Syntactic and semantic differences to Java
      1. Semicolons
      2. Import statements
      3. Class literals
      4. Visibility
      5. Exceptions
      6. Implicit return
      7. Default parameters
      8. Method dispatch
      9. Valid Java code that is not valid Groovy code
    3. Dynamic typed variables
    4. List and Map literals
      1. Lists
      2. Maps
      3. Ranges
      4. Immutability
      5. Maps as named arguments
      6. Set and array literals
    5. Truthiness
    6. Properties
      1. Accessing properties
      2. Properties of Java classes
    7. Strings in Groovy
      1. Templated strings
      2. Regular expression literals
      3. The “dollar-slashy” string
      4. Multi-line strings
    8. Operator overloading
      1. Unary math operators
      2. Comparison operators
      3. Mathematical operators
      4. Bitwise operators
      5. Shift operators
    9. Operators not present in Java
      1. Safe dereference operator
      2. Call operator
      3. Subscript operators
      4. Elvis operator
      5. Spaceship operator
      6. Spread operators
      7. Regular expression operators
      8. Field access operator
      9. Method pointer operator
    10. Operator-like Keywords
    11. Closures
      1. Defining closures
      2. Methods that accept closures
      3. Closure delegates
      4. Parameter and delegate type information
      5. Closures and Java 8 functional interfaces
    12. The GDK (Groovy Development Kit)
      1. Functional iterator methods
      2. Chained functional style
    13. AST transformations
    14. Summary
  22. Bibliography
  23. Index