Most developers would agree that writing automated tests is a good idea, but writing good, well-structured tests is still an elusive skill for many. For Java and Groovy developers, however, there’s good news. This practical guide shows you how to write concise and highly readable tests with Spock, the most innovative testing and specification framework for the JVM since JUnit.
Author Rob Fletcher takes you from Spock basics to advanced topics, using fully worked integration examples. Through the course of this book, you’ll build a simple web application—Squawker—that allows users to post short messages. You’ll discover how much easier it is to write automated tests with Spock’s straightforward and expressive language.
Start by learning how to write simple unit tests
Understand the lifecycle of Spock specifications and feature methods
Dive into interaction testing, using Spock’s intuitive syntax for dealing with mocks and stubs
Learn about parameterized tests—writing feature methods that run for multiple sets of data
Move into advanced topics, such as writing idiomatic Spock code and driving parameterized tests with file or database input
Learn how everything works together in a standalone, fully-worked, test-driven development example
Chapter 1The “Up and Running” Part
Running with the JUnit Runner
Running with Gradle
Running with Maven
Chapter 2Specification Basics
Anatomy of a Specification
An Introduction to Spock’s Blocks
The Specification Lifecycle
Chapter 3Spock Assertions
The Importance of Good Diagnostics
The Power Assert
Using Groovy for Effective Assertions
Grouping Assertions on the Same Object
Chapter 4Managing Resources
How (Not) to Approach Persistence Testing
Testing a Persistence Layer
Specifications and Inheritance
Chapter 5Interaction Testing
Asserting that a Method Is Called
Specifying Invocation Cardinality
Declaring the Return Value from a Mocked Method
Disallowing Unexpected Calls or “Strict Mocking”
Looser Matching with Wildcard Parameters
Defining Behavior for Mocked Methods
Removing Invocation Constraints with Stubs
Chapter 6Parameterized Specifications
Paramaterization in JUnit
Spock’s where: block
Separating Iteration Reporting by Using @Unroll
Multiple Data Pipes
Fields and Methods as Data Providers
Chapter 7Idiomatic Spock
The Grammar of Blocks
Method Parameter Capture with Mocks
Automatically Cleaning Up Resources
Chapter 8Advanced where: Blocks
A Primary Key Verifier
A Static Site Link Checker
Chapter 9Asynchronous Testing
Testing Nonblocking APIs by Using Callbacks
Polling for Eventual State
Asynchronous Interaction Testing
Chapter 10Extending Spock
The Message Timeline
Chapter 11Test-Driven Development: The Diamond Kata
Rob Fletcher is a Senior Software Engineer at Netflix, where he focuses on using automated testing as design tool. He has over 15 years of experience in the software industry, and has contributed to several open source projects including Groovy, Geb, Ratpack, and Grails.
The animal on the cover of Spock: Up and Running is a large treeshrew (Tupaia tana), a mammal found in Borneo, Sumatra, and other small islands in Indonesia and Malaysia. Tupaia is derived from tupai, the Malaysian word for squirrel. Despite their common name, they are not true shrews, and this particular species spends more time on the ground than any other treeshrew.
Large treeshrews have reddish-brown fur that becomes darker toward the tail. They are 7 to 13 inches long on average, and weigh between 5.5 to 11 pounds. Though they don't see well in daylight, their night vision is very good, and they also have an acute sense of smell and hearing. The animal lives in a forest habitat (primarily tropical), and does spend part of its time in trees. Most of their foraging is done on the forest floor, however, as they feed on fruit, earthworms, insect larvae, and arthropods like centipedes.
Treeshrews have the highest brain-to-body-mass ratio among mammals, including humans. Such a high ratio is common in smaller animals such as mice and bats, but is only a rough indicator of intelligence.
Many of the animals on O'Reilly covers are endangered; all of them are important to the world. To learn more about how you can help, go to animals.oreilly.com.