Java Testing with Spock
Total Page:16
File Type:pdf, Size:1020Kb
Konstantinos Kapelonis FOREWORD BY Luke Daley MANNING Java Testing with Spock Licensed to Stephanie Bernal <[email protected]> ii Licensed to Stephanie Bernal <[email protected]> Java Testing with Spock KONSTANTINOS KAPELONIS MANNING SHELTER ISLAND Licensed to Stephanie Bernal <[email protected]> iv For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2016 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without elemental chlorine. Manning Publications Co. Development editors: Susan Conant 20 Baldwin Road Dan Maharry PO Box 761 Technical development editor: Keith Conant Shelter Island, NY 11964 Copyeditor: Sharon Wilkey Proofreaders: Melody Dolab Toma Mulligan Technical proofreader: Francesco Bianchi Typesetter: Marija Tudor Cover designer: Marija Tudor ISBN: 9781617292538 Printed in the United States of America 12345678910–EBM–212019181716 Licensed to Stephanie Bernal <[email protected]> To Maria, for enduring my endless writing sessions Licensed to Stephanie Bernal <[email protected]> Licensed to Stephanie Bernal <[email protected]> brief contents PART 1FOUNDATIONS AND BRIEF TOUR OF SPOCK ................... 1 1 ■ Introducing the Spock testing framework 3 2 ■ Groovy knowledge for Spock testing 31 3 ■ A tour of Spock functionality 62 PART 2STRUCTURING SPOCK TESTS ...................................... 89 4 ■ Writing unit tests with Spock 91 5 ■ Parameterized tests 127 6 ■ Mocking and stubbing 157 PART 3SPOCK IN THE ENTERPRISE ...................................... 191 7 ■ Integration and functional testing with Spock 193 8 ■ Spock features for enterprise testing 224 vii Licensed to Stephanie Bernal <[email protected]> viii BRIEF CONTENTS Licensed to Stephanie Bernal <[email protected]> contents foreword xv preface xvii acknowledgments xix about this book xxi about the cover illustration xxiv PART 1FOUNDATIONS AND BRIEF TOUR OF SPOCK ....... 1 Introducing the Spock testing framework 3 1 1.1 What is Spock? 4 Mocking and stubbing 5 ■ Behavior-driven development 5 Spock’s design features 5 ■ Spock’s coding features 6 1.2 The need for a testing framework 8 Spock as an enterprise-ready test framework 9 ■ Common ways to handle enterprise complexity 13 1.3 Spock: the groovier testing framework 15 Asserts vs. Assertions 15 ■ Agnostic testing of Java and Groovy 15 ■ Taking advantage of Groovy tricks in Spock tests 16 1.4 Getting an overview of Spock’s main features 16 Enterprise testing 17 ■ Data-driven tests 17 Mocking and stubbing 17 ix Licensed to Stephanie Bernal <[email protected]> x CONTENTS 1.5 A first look at Spock in action 18 A simple test with JUnit 18 ■ A simple test with Spock 19 Inspecting failed tests with Spock 20 1.6 Spock’s position in the Java ecosystem 23 Making Spock Groovy 24 ■ Adding Spock tests to existing projects that have JUnit tests 25 ■ Spock adoption path in a Java project 26 1.7 Comparing Spock and JUnit 27 Writing concise code with Groovy syntax 27 ■ Mocking and stubbing with no external library 27 ■ Using English sentences in Spock tests and reports 28 1.8 Summary 30 Groovy knowledge for Spock testing 31 2 2.1 What you need to know about Groovy 32 Groovy as a companion to Java 34 ■ Accessing Java classes in a Groovy script 37 ■ Declaring variables and methods in Groovy 38 ■ Writing less code with Groovy 40 2.2 Groovy Power assert as a replacement for JUnit asserts 41 Understanding how Groovy handles asserts 41 ■ Using Groovy assertions in Spock tests 44 2.3 Groovy features useful to Spock tests 46 Using map-based constructors 46 ■ Using maps and lists in Groovy 47 ■ Interpolating text with Groovy strings 50 2.4 Reading a test dataset from an external source 51 Reading a text file 51 ■ Reading an XML file 52 Reading a JSON file 53 2.5 Advanced Groovy features useful to testing 54 Using Groovy closures 55 ■ Creating test input with ObjectGraphBuilders 56 ■ Creating test input with Expando 58 2.6 Summary 61 A tour of Spock functionality 62 3 3.1 Introducing the behavior-testing paradigm 63 The setup-stimulate-assert structure of JUnit 65 ■ The given- when-then flow of Spock 67 Licensed to Stephanie Bernal <[email protected]> CONTENTS xi 3.2 Handling tests with multiple input sets 70 Existing approaches to multiple test-input parameters 72 Tabular data input with Spock 74 3.3 Isolating the class under test 76 The case of mocking/stubbing 76 ■ Stubbing fake objects with Spock 79 ■ Mocking collaborators 80 ■ Examining interactions of mocked objects 83 ■ Combining mocks and stubs in parameterized tests 86 3.4 Summary 87 PART 2STRUCTURING SPOCK TESTS .......................... 89 Writing unit tests with Spock 91 4 4.1 Understanding Spock from the ground up 91 A simple test scenario 92 ■ The given: block 94 ■ The setup: block 95 ■ The when: block 96 ■ The then: block 98 The and: block 99 ■ The expect: block 103 ■ The cleanup: block 104 4.2 Converting requirements to Spock tests 105 Explaining the feature examined in a Spock test 105 Marking the class under test inside a Spock test 106 Describing the Spock unit test as a whole 107 Revising our view of a Spock test 108 4.3 Exploring the lifecycle of a Spock test 109 Setup and cleanup of a feature 109 ■ Setup and cleanup of a specification 110 ■ Long-lived objects with the @Shared annotation 112 ■ Use of the old() method 113 4.4 Writing readable Spock tests 115 Structuring Spock tests 115 ■ Ensuring that Spock tests are self-documenting 116 ■ Modifying failure output 117 Using Hamcrest matchers 119 ■ Grouping test code further 122 4.5 Summary 125 Parameterized tests 127 5 5.1 Detecting the need for parameterized tests 128 What are parameterized tests? 130 Licensed to Stephanie Bernal <[email protected]> xii CONTENTS 5.2 Using the where: block 131 Using data tables in the where: block 133 ■ Understanding limitations of data tables 134 ■ Performing easy maintenance of data tables 135 ■ Exploring the lifecycle of the where: block 137 ■ Using the @Unroll annotation for reporting individual test runs 139 ■ Documenting parameterized tests 141 ■ Using expressions and statements in data tables 143 5.3 Using data pipes for calculating input/output parameters 144 Dynamically generated parameters 145 ■ Parameters that stay constant 147 ■ Parameters that depend on other parameters 147 5.4 Using dedicated data generators 148 Writing a custom data generator 150 ■ Using multivalued data iterators 152 5.5 Working with third-party data generators 155 5.6 Summary 156 Mocking and stubbing 157 6 6.1 Using fake collaborators 158 Using fake collaborators to isolate a class in unit tests 158 Faking classes in Spock: mocks and stubs 159 ■ Knowing when to use mocks and stubs 160 ■ Exploring a sample application for an electronic shop system 161 6.2 Controlling input to the class under test with stubs 163 Basic stubbing of return values 163 ■ Matching arguments leniently when a stubbed method is called 166 ■ Using sequential stubs with different responses for each method call 167 ■ Throwing exceptions when a stubbed method is called 168 ■ Using dynamic stubs that check arguments when responding 169 ■ Returning stubs from the responses of other stubs 172 6.3 Mocks: verifying values returned from the class under test 173 All capabilities of stubs exist in mocks as well 174 ■ Simple mocking—examining whether a method was called 174 Verifying order of interactions 176 ■ Verifying number of method calls of the mocked class 177 ■ Verifying noninteractions for multiple mocked classes 179 ■ Verifying types of arguments when a mocked method is called 181 Verifying arguments of method calls from mocked classes 182 6.4 Putting it all together: credit card charging in two steps 184 Licensed to Stephanie Bernal <[email protected]> CONTENTS xiii 6.5 Architecture considerations for effective mocking/ stubbing 188 Designing testable code that allows painless mocking 188 Understanding lenient vs. strict mocks 188 6.6 Summary 189 PART 3SPOCK IN THE ENTERPRISE .......................... 191 Integration and functional testing with Spock 193 7 7.1 Unit tests vs. integration tests vs. functional tests 194 Characteristics of the test categories 195 ■ The testing pyramid 197 ■ Spock support for integration and functional testing 198 ■ Source code organization of the examples 199 7.2 Integration testing with Spock 199 Testing a Spring application 199 ■ Narrowing down the Spring context inside Spock tests 202 ■ Directly accessing the database with Groovy SQL 204