What Readers Are Saying About The Cucumber for Java Book This book is not just for programmers but for testers as well. It goes beyond using Cucumber; it gives testing guidelines as well and hints and tips to avoid bad tests. ➤ Janet Gregory Author, Agile Testing: A Practical Guide for Testers (with Lisa Crispin) If you read this book in the morning, then find a stakeholder quickly enough, you’ll be writing effective scenarios with that person in the afternoon. Start today. ➤ J.B. Rainsberger Author, JUnit Recipes The best thing about this book: it will help you identify WHY you may want to use Cucumber, and it will guide you to good ways to do it in collaboration with your whole team. ➤ Lisa Crispin Author, Agile Testing: A Practical Guide for Testers (with Janet Gregory) This is an excellent introduction to using Cucumber on the JVM that guides the reader clearly through the complexities of the API and away from common pitfalls in its application to real projects. ➤ Nat Pryce Author, Growing OO Software Guided By Tests (with Steve Freeman) Not only does [this book] go deep on the technical details of using Cucumber ef- fectively in a Java environment, it also covers the broader issues of how to succeed in driving software implementation through readable examples of system behavior, and as such it will be a valuable reference for the whole team. ➤ David Evans Author, Fifty Quick Ideas to Improve Your User Stories (with Gojko Adžić) [If you] implement an application in a test-first manner, taking small and careful steps and alternating implementation and refactoring phases, your tests will tell you what to do next. So listen to your tests and listen to this book! ➤ Gáspár Nagy Creator of SpecFlow - Cucumber for .NET This book will teach you all you need to know to get started with Cucumber on the Java platform...although the authors make sure that nonprogrammers can follow along. It will find its place on my bookshelf and in the book recommendations I give out in my training classes. ➤ Markus Gaertner Author, ATDD by Example This is a great reference guide for software project builds maintainers, because the authors carefully address the integration of Cucumber with other Java platform favorites such as JDBC, databases, dependency injection containers such as CDI and Spring Framework, and REST server-side endpoints. ➤ Peter Pilgrim Author, The Java EE 7 Developer Handbook The Cucumber for Java Book meets an important need—the large Java community has a great tool in Cucumber but very limited documentation on how to use it effectively. This is the book I’ll recommend to my Java clients who want to hit the ground running with Cucumber. ➤ Richard Lawrence BDD Trainer and Coach, Agile for All A gentle yet complete introduction to Cucumber on the JVM. An excellent step- by-step guide with attention to detail. ➤ Paul Grenyer Founder of Naked Element and NorDevCon The material [in this book] is built on real-life experiences with appreciation for the everyday problems we as testers/software architects/managers face both in- ternally and externally. ➤ Márton Mészáros Founder of Tailored Tunes and Test Automation Specialist The authors get the combination of technical and nontechnical lessons across very well. A very good read for the novice and experienced BDD follower alike. ➤ Craig Harrison Software Engineer The Cucumber for Java Book Behaviour-Driven Development for Testers and Developers Seb Rose Matt Wynne Aslak Hellesøy The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at https://pragprog.com. The team that produced this book includes: Jacquelyn Carter (editor) Potomac Indexing, LLC (indexer) Liz Welch (copyeditor) Dave Thomas (typesetter) Janet Furlow (producer) Ellie Callahan (support) For international rights, please contact [email protected]. Copyright © 2015 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-941222-29-4 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—February 2015 Contents Foreword . . . . . . . . . . . . . . xi Acknowledgments . . . . . . . . . . . xiii Preface . . . . . . . . . . . . . . xv Part I — Cucumber Fundamentals 1. Why Cucumber? . . . . . . . . . . . . 3 Automated Acceptance Tests 4 Behaviour-Driven Development 4 Living Documentation 6 How Cucumber Works 7 What We Just Learned 8 2. First Taste . . . . . . . . . . . . . 11 Understanding Our Goal 11 Creating a Feature 12 Creating Step Definitions 17 Implementing Our First Step Definition 19 Changing Cucumber’s Output 20 Testing Our Checkout Class 21 Adding an Assertion 23 Making It Pass 25 What We Just Learned 28 3. Gherkin Basics . . . . . . . . . . . . 31 What’s Gherkin For? 31 Format and Syntax 34 Feature 35 Scenario 36 Contents • viii Comments 40 Spoken Languages 41 What We Just Learned 42 4. Step Definitions: From the Outside . . . . . . . 45 Steps and Step Definitions 46 Capturing Arguments 51 Multiple Captures 55 Flexibility 56 Returning Results 58 What We Just Learned 66 5. Expressive Scenarios . . . . . . . . . . 69 Background 69 Data Tables 72 Scenario Outline 79 Too Much Information 84 Doc Strings 86 Staying Organized with Tags and Subfolders 87 What We Just Learned 90 6. Keeping Your Cucumbers Sweet . . . . . . . . 91 Feeling the Pain 92 Working Together 95 Caring for Your Tests 102 Stop the Line and Defect Prevention 110 What We Just Learned 112 Part II — A Worked Example 7. Step Definitions: On the Inside . . . . . . . . 117 Sketching Out the Domain Model 118 Staying Honest with Transforms 123 Adding Custom Helper Methods 127 Organizing the Code 134 Dependency Injection 137 What We Just Learned 138 8. Support Code . . . . . . . . . . . . 141 Fixing the Bug 142 Bootstrapping the User Interface 149 Contents • ix Making the Switch 151 Using Hooks 156 Getting to Green 160 What We Just Learned 166 9. Message Queues and Asynchronous Components . . . 169 Our New Asynchronous Architecture 169 How to Synchronize 170 Implementing the New Architecture 172 Fixing the Flickering Scenario 179 What We Just Learned 184 10. Databases . . . . . . . . . . . . . 185 Iterative Database Development 185 Refactoring to Use a Database 188 Reading and Writing to the Database 194 Cleaning the Database with Transactions 199 Cleaning the Database with Truncation 202 What We Just Learned 204 11. Simplifying Design with Dependency Injection . . . . 205 DI and Cucumber 205 Improving Our Design Using DI 207 PicoContainer Is Almost Invisible 213 Moving to Guice 214 Spring in Your Steps 218 CDI with Weld 221 What We Just Learned 223 12. Working with Web Applications . . . . . . . . 225 Selenium WebDriver 225 Handling Failure 228 Reusing the Browser 233 Ajax 235 What We Just Learned 242 13. Keeping Your Features Fast . . . . . . . . 245 Partitioning Features and Scenarios 245 What Sort of Tests? 248 Environment-Specific Step Definitions 250 Changing Step Definitions Using Tags 253 What We Just Learned 256
Description: