Spock – Up and Running
Writing Expressive Tests in Java and Groovy
Paperback Engels 2017 1e druk 9781491923290Samenvatting
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
Specificaties
Lezersrecensies
Inhoudsopgave
Part 1: Spock 101
1. The “Up and Running” Part
Installation
Running with the JUnit Runner
Running with Gradle
Running with Maven
2. Specification Basics
Anatomy of a Specification
An Introduction to Spock’s Blocks
The Specification Lifecycle
Block Descriptions
Summary
3. Spock Assertions
The Importance of Good Diagnostics
The Power Assert
Using Groovy for Effective Assertions
Expecting Exceptions
Grouping Assertions on the Same Object
Summary
4. Managing Resources
How (Not) to Approach Persistence Testing
Testing a Persistence Layer
Specifications and Inheritance
Summary
5. Interaction Testing
Asserting that a Method Is Called
Specifying Invocation Cardinality
Declaring the Return Value from a Mocked Method
Parameter Predicates
Disallowing Unexpected Calls or “Strict Mocking”
Looser Matching with Wildcard Parameters
Defining Behavior for Mocked Methods
Removing Invocation Constraints with Stubs
Summary
6. Parameterized Specifications
Paramaterization in JUnit
Spock’s where: block
Separating Iteration Reporting by Using @Unroll
Multiple Data Pipes
Data Tables
Fields and Methods as Data Providers
Derived Values
Summary
Advanced Spock
7. Idiomatic Spock
Organizing Tests
Well-Factored Specifications
The Grammar of Blocks
Method Parameter Capture with Mocks
@Stepwise Specifications
Conditional Specifications
Automatically Cleaning Up Resources
Documenting Specifications
Summary
8. Advanced where: Blocks
A Primary Key Verifier
A Static Site Link Checker
Summary
9. Asynchronous Testing
Blocking Constructs
Testing Nonblocking APIs by Using Callbacks
Polling for Eventual State
Asynchronous Interaction Testing
Summary
10. Extending Spock
The Message Timeline
Hamcrest Matchers
JUnit Rules
Spock Extensions
Global Extensions
Summary
11. Test-Driven Development: The Diamond Kata
The Diamond Kata
Implementation
Summary
Part 2: Integrating Spock
12. Spock and Spring
What Do We Mean by “Integration Tests”?
Spring Compatibility
Testing Annotation-Driven Transactions
Mock and Stub Beans
Declarative Test Data
Tests in Transactions
Summary
13. Testing REST APIs
Defining the Application URL
A First Specification
Web Application Lifecycle
Creating Data for End-to-End Tests
Requests with Data
Testing for HTTP Errors
Verifying Response Data
Multiple HTTP Requests in a Feature Method
Backdoors or “Pure” API Testing?
Handling Redirect Responses
REST API Security
Summary
14. End-to-End Web Testing
Geb
Getting Started with Geb
A First Frontend Test
Introducing Page Models
Authentication
Modules
Parameterized Pages
Interacting with Forms
Form Validation
15. Testing JavaScript Using Spock
Why Test JavaScript from the JVM?
Setting Up Nashorn for Tests
Calling JavaScript Functions
Testing an Isomorphic View Component
Passing Mocks to JavaScript
Rendering a View with the Handlebars Library
Summary
16. Test Driving an RxJava Application
Tests, Behaviors, and Properties
The Skeleton Implementation
Test Driving the Implementation
Polling at Regular Intervals
Final Implementation
Appendix: Groovy Primer
History
Syntactic and Semantic Differences to Java
Dynamic-Typed Variables
List and Map literals
Truthiness
Properties
Strings in Groovy
Operator Overloading
Operators Not Present in Java
Operator-Like Keywords
Closures
The Groovy Development Kit
AST Transformations
Summary
Index
Rubrieken
- advisering
- algemeen management
- coaching en trainen
- communicatie en media
- economie
- financieel management
- inkoop en logistiek
- internet en social media
- it-management / ict
- juridisch
- leiderschap
- marketing
- mens en maatschappij
- non-profit
- ondernemen
- organisatiekunde
- personal finance
- personeelsmanagement
- persoonlijke effectiviteit
- projectmanagement
- psychologie
- reclame en verkoop
- strategisch management
- verandermanagement
- werk en loopbaan