Spock: Up and Running. Writing Expressive Tests in Java and Groovy - Helion
ISBN: 978-14-919-2324-5
stron: 372, Format: ebook
Data wydania: 2017-05-08
Księgarnia: Helion
Cena książki: 194,65 zł (poprzednio: 226,34 zł)
Oszczędzasz: 14% (-31,69 zł)
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
Osoby które kupowały "Spock: Up and Running. Writing Expressive Tests in Java and Groovy", wybierały także:
- Wprowadzenie do Javy. Programowanie i struktury danych. Wydanie XII 193,23 zł, (59,90 zł -69%)
- Spring i Spring Boot. Kurs video. Testowanie aplikacji i bezpiecze 129,00 zł, (51,60 zł -60%)
- Metoda dziel i zwyci 89,00 zł, (35,60 zł -60%)
- JavaFX. Kurs video. Wzorce oraz typy generyczne 79,00 zł, (31,60 zł -60%)
- Platforma Xamarin. Kurs video. Poziom drugi. Zaawansowane techniki tworzenia aplikacji cross-platform 99,00 zł, (39,60 zł -60%)
Spis treści
Spock: Up and Running. Writing Expressive Tests in Java and Groovy eBook -- spis treści
- Introduction
- Meet Spock
- A Short Background on Groovy
- Who Is This Book For?
- About This Book
- Navigating This Book
- Conventions Used in This Book
- Using Code Examples
- OReilly Safari
- How to Contact Us
- Acknowledgments
- I. Spock 101
- 1. The Up and Running Part
- Installation
- Running with the JUnit Runner
- Running with Gradle
- When Groovy Is Used in the Project
- Synchronizing Groovy Versions Between Main and Test Classpaths
- Running with Maven
- 2. Specification Basics
- Anatomy of a Specification
- Why Use Quoted Strings for Feature Method Names?
- An Introduction to Spocks Blocks
- Block Taxonomy
- Basic Block Usage
- The Specification Lifecycle
- Fixture methods: setup and cleanup
- Block Descriptions
- and: blocks
- A Note on Comprehensibility
- Summary
- Comparison with JUnit
- Anatomy of a Specification
- 3. Spock Assertions
- The Importance of Good Diagnostics
- The Power Assert
- Type Information in Power Asserts
- Using Groovy for Effective Assertions
- Using List Comprehensions and List Literals
- Asserting Something About Every Item in a Collection
- Expressive Assertions with Groovy Operators
- Expecting Exceptions
- Interrogating the Exception
- Grouping Assertions on the Same Object
- Summary
- 4. Managing Resources
- How (Not) to Approach Persistence Testing
- Testing a Persistence Layer
- Managing Resources with the Spock Lifecycle
- Specifications and Inheritance
- Execution Order of Lifecycle Methods in an Inheritance Hierarchy
- 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
- Wildcard Shortcuts
- Defining Behavior for Mocked Methods
- Removing Invocation Constraints with Stubs
- When Should I Use a Mock and When Should I Use a Stub?
- Mixing Mocks and Stubs
- Summary
- 6. Parameterized Specifications
- Paramaterization in JUnit
- Spocks where: block
- Separating Iteration Reporting by Using @Unroll
- Adding @Unroll Descriptions
- @Unroll Tokens in Feature Method Names
- Class Level @Unroll Annotations
- Multiple Data Pipes
- Data Tables
- Fields and Methods as Data Providers
- Derived Values
- Clarifying @Unroll Expressions
- Summary
- II. Advanced Spock
- 7. Idiomatic Spock
- Organizing Tests
- The Test-per-Method Antipattern
- Well-Factored Specifications
- Sharing Helper Methods
- Sharing Methods by Using import static
- Sharing Methods with Groovy Traits
- Sharing Methods with Delegation
- Helper Methods and Assertions
- Helper Methods and Mock Interactions
- Comparing Before and After Values
- The Grammar of Blocks
- Separation of Logic and Data with where: Blocks
- Using when/then or given/expect
- Separating Preconditions
- Separating Preconditions from Actions
- Method Parameter Capture with Mocks
- Using a Closure as a Parameter Predicate
- Using Hamcrest for Parameter Predicates
- Parameter Capture Using a Mock Responder
- @Stepwise Specifications
- How to Use @Stepwise
- Disadvantages of @Stepwise
- Conditional Specifications
- Marking Pending Features
- Selectively Executing Tests
- Automatically Cleaning Up Resources
- Documenting Specifications
- Summary
- Organizing Tests
- 8. Advanced where: Blocks
- A Primary Key Verifier
- A Static Site Link Checker
- JBake Configuration
- A Data Pipe Driven by a File
- A Filtered Data Pipe
- Relative Filepaths
- Summary
- 9. Asynchronous Testing
- Blocking Constructs
- BlockingVariable
- BlockingVariables
- Testing Nonblocking APIs by Using Callbacks
- Using BlockingVariable in a Callback
- Using a Reference to BlockingVariable.set as a Callback
- Awaiting Multiple Values with BlockingVariables
- Improving diagnostics
- Polling for Eventual State
- Using PollingConditions as a Delegate
- Asynchronous Interaction Testing
- Asynchronous Parameter Capture
- Extending BlockingVariable
- Summary
- Blocking Constructs
- 10. Extending Spock
- The Message Timeline
- Hamcrest Matchers
- Hamcrest Support in Spock
- JUnit Rules
- Reusable Data Cleanup
- Simplifying the Rule by Using ExternalResource
- Making the Cleanup Logic Generic
- Spock Extensions
- Data Cleanup as a Spock Extension
- Further Steps
- Global Extensions
- Taking it Further
- Summary
- 11. Test-Driven Development: The Diamond Kata
- The Diamond Kata
- Implementation
- First Step: The Pathological Case
- The Simplest Valid Diamond
- Enforcing Invariants
- Matrix Content
- Symmetry
- Eliminating the Assertion Loop
- Adding Padding to the Rows
- Refinement
- Summary
- III. Integrating Spock
- 12. Spock and Spring
- What Do We Mean by Integration Tests?
- Container Tests
- Spring Compatibility
- @SpringBootTest
- Testing Annotation-Driven Transactions
- Tracking Who Is Mentioned in a Message
- Non-Spring-Dependent Tests
- Testing a Transaction Rollback
- Setting Up the Spring Application
- A First Integration Test
- Convenience and Cost
- Unexpected Side Effects
- Forcing Spring to Re-Create the Application Context
- A Better Way to Force Errors
- Mock and Stub Beans
- Mention Events
- Mock Beans Before Spock 1.1
- Mock Bean Delegates
- Detached Mocks in the Spring Context
- Spring Autoconfiguration and Test Configuration Classes
- Mock Beans in XML Configuration
- Replacing Application Beans with Mocks
- Declarative Test Data
- Using the @Sql Annotation for Data Fixtures
- Global Fixture Data
- Cleaning Up Test Data Spring-Style
- Tests in Transactions
- Propagated Transactions
- Summary
- What Do We Mean by Integration Tests?
- 13. Testing REST APIs
- Defining the Application URL
- A First Specification
- Web Application Lifecycle
- Creating Data for End-to-End Tests
- Bleeding Data Between Tests
- Creating Test Data on Demand
- Cleaning Up Test Data
- Requests with Data
- Fixture Methods
- Testing for HTTP Errors
- Verifying Response Data
- Typed Response Entities
- Multiple HTTP Requests in a Feature Method
- Backdoors or Pure API Testing?
- Handling Redirect Responses
- Verifying Redirect Details
- REST API Security
- Authentication versus Authorization
- Summary
- 14. End-to-End Web Testing
- Geb
- Getting Started with Geb
- A First Frontend Test
- Introducing Page Models
- Setting the Base URL
- Gebs Automatic Delegation
- Authentication
- Encapsulating Interactions by Using Methods
- Modules
- Parameterized Pages
- Interacting with Forms
- Accessing Form Elements by Name
- Encapsulating Interactions with Methods
- Form Validation
- 15. Testing JavaScript Using Spock
- Why Test JavaScript from the JVM?
- Setting Up Nashorn for Tests
- Calling JavaScript Functions
- JavaScript Functions as Objects
- Specifying this in a JavaScript Function
- Testing an Isomorphic View Component
- An Invocable Script Engine as a Delegate
- Passing Mocks to JavaScript
- Rendering a View with the Handlebars Library
- Compiling Handlebars Templates
- Testing Template Rendering
- Handling Properties That Dont Convert to JavaScript
- Registering Handlebars Helper Functions
- Composing Templates with Handlebars Partials
- Passing Iterable Values to JavaScript
- Summary
- 16. Test Driving an RxJava Application
- Tests, Behaviors, and Properties
- The Skeleton Implementation
- Test Driving the Implementation
- Defining the Behavior
- A First Test
- Polling at Regular Intervals
- No Sleep `Til the Test Goes Green
- Testing Subscription to the Stream
- Unchunking the Message Stream
- Requesting Messages Since the Last Received
- Recovering from Polling Errors
- Recovering from Downstream Errors
- Final Implementation
- A. Groovy Primer
- History
- Syntactic and Semantic Differences to Java
- Semicolons
- Import Statements
- Class Literals
- Visibility
- Exceptions
- Implicit Return
- Default Parameters
- Method Dispatch
- Valid Java Code That Is Not Valid Groovy Code
- Array initialization
- Multiple for loop variables or increments
- The dowhile loop
- Try with resource (Java 7+)
- Lambdas (Java 8+)
- Method references (Java 8+)
- Dynamic-Typed Variables
- List and Map literals
- Lists
- Indexing lists
- Maps
- Programmatically assigning map keys
- Accessing map values
- Ranges
- Immutability
- Maps as Named Arguments
- Set and Array Literals
- Lists
- Truthiness
- Properties
- Accessing Properties
- Properties of Java Classes
- Strings in Groovy
- Templated Strings
- Regular Expression Literals
- The Dollar-Slashy String
- Multiline Strings
- Operator Overloading
- Unary Math Operators
- Comparison Operators
- Mathematical Operators
- Bitwise Operators
- Shift Operators
- Operators Not Present in Java
- Safe Dereference Operator
- Call Operator
- Subscript Operators
- Elvis Operator
- Spaceship Operator
- Spread Operators
- Spreading parameters
- Regular Expression Operators
- Field Access Operator
- Method Pointer Operator
- Operator-Like Keywords
- Closures
- Defining Closures
- Optional parameter types
- The implicit closure parameter
- Methods That Accept Closures
- Closure Delegates
- Parameter and Delegate Type Information
- Closures and Java 8 Functional Interfaces
- Defining Closures
- The Groovy Development Kit
- Functional Iterator Methods
- each(Closure)
- eachWithIndex(Closure)
- find(Closure)
- findAll(Closure)
- collect(Closure)
- any(Closure)
- every(Closure)
- Chained Functional Style
- Functional Iterator Methods
- AST Transformations
- Summary
- Bibliography
- Index