Growing Object-Oriented Software, Guided by Tests Steve ... - GBV

5 downloads 90 Views 126KB Size Report
Growing. Object-Oriented. Software,. Guided by Tests. Steve Freeman and Nat Pryce l'Y"Addison-Wesley. Upper Saddle River, NJ • Boston • Indianapolis • San  ...
Growing Object-Oriented Software, Guided by Tests Steve Freeman and Nat Pryce

l'Y" Addison-Wesley Upper Saddle River, NJ • Boston • Indianapolis • San Francisco NewYork • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City

Contents

Foreword

xv

Preface

xvii

Acknowledgments

xxi

About the Authors

xxiii

Part I: Introduction

1

Chapter 1: What Is the Point of Test-Driven Development? Software Development as a Learning Process Feedback Is the Fundamental Tool Practices That Support Change Test-Driven Development in a Nutshell The Bigger Picture Testing End-tc-End Levels of Testing External and Internal Quality

3 3 4 5 6 7 8 9 10

Chapter 2: Test-Driven Development with Objects A Web of Objects Values and Objects Follow the Messages Tell, Don't Ask But Sometimes Ask Unit-Testing the Collaborating Objects Support foe TDD with Mock Objects

13 13 13 14 17 17 18 19

IX

CONTENTS

Chapter 3: An Introduction to the Tools Stop Me If You've Heard This One Before A Minimal Introduetion to JUnit 4 Hamerest Matehers and assertThatO jMoek2: Moek Objects Part II: The Process of Test-Driven Development Chapter 4: Kick-Starting the Test-Driven Cycle Introduction First, Test a Walking Skeleton Deeiding the Shape of the Walking Skeleton Build Soure es of Feedback Expose Uncertainty Early

21 21 21 24 25 29 31 31 32 33 35 36

Chapter 5: Maintaining the Test-Driven Cycle 39 Introduction 39 Start Eaeh Feature with an Aeceptanee Test 39 Separate Tests That Measure Progress from Those That Cateh Regressions 40 Start Testing with the Simplest Sueeess Case 41 Write the Test That You'd Want to Read 42 Wateh the Test Fail 42 43 Develop from the Inputs to the Outputs Unit-Test Behavior, Not Methods 43 Listen to the Tests 44 Tuning the Cycle 45 Chapter 6: Object-Oriented Style Introduetion Designing for Maintainability Internals vs, Peers No And's, Or's, or But's Objeet Peer Stereotypes Cornposite Simpler Than the Sum of Its Parts Context Independence Hiding the Right Information An Opinionated View

47 47 47 50 51 52 53 54 55 56

Chapter 7: Achieving Object-Oriented Design How Writing a Test First Helps the Design Cornmunication over Classification

57 57 58

CONTENTS

Value Types Where Do Objects Come From? Identify Relationships with Interfaces Refactor Interfaces Too Compose Objects to Describe System Behavior Building Up to Higher-Level Programming And What about Classes? Chapter 8: Building on Third-Party Code Introduction Only Mock Types That You Own Mock Application Objects in Integration Tests Part III: A Worked Example

59 60 63 63 64 65 67 69 69 69 71 73

Chapter 9: Commissioning an Auction Sniper To Begin at the Beginning Communicating with an Auction Getting There Safely This Isn't Real

75 75 78 79 81

Chapter 10: The Walking Skeleton Get the Skeleton out of the Closet Our Very First Test Some Initial Choices

83 83 84 86

Chapter 11: Passing the First Test Building the Test Rig Failing and Passing the Test The Necessary Minimum

89 89 95 102

Chapter 12: Getting Ready to Bid An Introduction to the Market A Test for Bidding The AuctionMessageTranslator Unpacking a Price Message Finish the Job

105 105 106 112 118 121

Chapter 13: The Sniper Makes a Bid Introducing AuctionSniper Sending a Bid Tidying Up the Implementation

123 123 126 131

CONTENTS

Defer Decisions Emergent Design

136 137

Chapter 14: The Sniper Wins the Auction First, a Failing Test Who Knows about Bidders? The Sniper Has More to Say The Sniper Acquires Some State The Sniper Wins Making Steady Progress

139 139 140 143 144 146 148

Chapter 15: Towards a Real User Interface A More Realistic Implementation Displaying Price Details Simplifying Sniper Events Follow Through Final Polish Observations

149 149 152 159 164 168 171

Chapter 16: Sniping for Multiple Items Testing for Multiple Items Adding Items through the User Interface Observations

175 175 183 189

Chapter 17: Teasing Apart Main Finding a Role Extracting the Chat Extracting the Connection Extracting the SnipersTableMode1 Observations

191 191 192 195 197 201

Chapter 18: Filling In the Details A More Useful Application Stop When We've Had Enough Observations

205 205 205 212

Chapter 19: Handling Failure What If It Doesn't Work? Detecting rhe Failure Displaying the Failure Disconnecting the Sniper Recording the Failure Observations

215 215 217 218 219 221 225

CONTENTS

Part IV: Sustainable Test-Driven Development

227

Chapter 20: Listening to the T ests Introduction I Need to Mock an Object I Can't Replace (without Magie) Logging Is a Feature Mocking Concrete Classes Don't Mock Values Bloated Constructor Confused Object Too Many Dependencies Too Many Expectations What the Tests Will Tell Us (If We're Listening)

229 229 230 233 235 237 238 240 241 242 244

Chapter 21: Test Readability Introduction Test Names Describe Features Canonical Test Structure Streamline the Test Code Assertions and Expectations Literals and Variables

247 247 248 251 252 254 255

Chapter 22: Constructing Complex Test Data Introduction Test Data Builders Creating Similar Objects Combining Builders Emphasizing the Domain Model with Factory Methods Removing Duplication at the Point of Use Communication First

257 257 258 259 261 261 262 264

Chapter 23 : Test Diagnostics Design to Fail SmalI, Focused, Well-Named Tests Explanatory Assertion Messages Highlight Detail with Matchers Self-Describing Value Obviously Canned Value Tracer Object Explicitly Assert That Expectations Were Satisfied Diagnostics Are a First-Class Feature

267 267 268 268 268 269 270 270 271 271

CONTENTS

Chapter 24: Test Flexibility Introduction Test for Information, Not Representation Precise Assertions Precise Expectations "Guinea Pig" Objects Part V: Advanced Topics

273 273 274 275 277 284 287

Chapter 25: Testing Persistence Introduction Isolate Tests That Affect Persistent State Make Tests Transaction Boundaries Explicit Testing an Object That Performs Persistence Operations Testing That Objects Can Be Persisted But Database Tests Are S-l-o-w!

289 289 290 292 294 297 300

Chapter 26: Unit Testing and Threads Introduction Separating Functionality and Concurrency Policy Unit-Testing Synchronization Stress-Testing Passive Objects Synchronizing the Test Thread with Background Threads The Limitations of Unit Stress Tests

301 301 302 306 311 312 313

Chapter 27: Testing Asynchronous Code Introduction Sampling or Listening Two Implementations Runaway Tests Lost Updates Testing That an Action Has No Effect Distinguish Synchronizations and Assertions Externalize Event Sources

315 315 316 318 322 323 325 326 326

Afterword: ABrief History of Mock Objects

329

Appendix A: jMock2 Cheat Sheet

335

Appendix B: Writing a Hamcrest Mateher

343

Bibliography

347

Index

349