Learn how to improve your Java coding skills using unit testing. Despite its name, unit testing is really a coding technique, not a testing technique. Unit testing is done by programmers, for programmers. It's primarily for our benefit: we get improved confidence in our code, better ability to make deadlines, less time spent in the debugger, and less time beating on the code to make it work correctly. This book shows how to write tests, but more importantly, it goes where other books fear to tread and gives you concrete advice and examples of what to test--the common things that go wrong in all of our programs. Discover the tricky hiding places where bugs breed, and how to catch them using the freely available JUnit framework. It's easy to learn how to think of all the things in your code that are likely to break. We'll show you how with helpful mnemonics, summarized in a handy tip sheet (also available from our www pragmaticprogrammer.com website) to help you remember all this stuff.
With this book you will: Write better code, and take less time to write it; Discover the tricky places where bugs breed; Learn how to think of all the things that could go wrong; Test individual pieces of code without having to include the whole project; Test effectively with the whole team We'll also cover how to use Mock Objects for testing, how to write high quality test code, and how to use unit testing to improve your design skills. We'll show you frequent "gotchas"--along with the fixes--to save you time when problems come up. We'll show you how with helpful mnemonics, summarized in a handy tip sheet (also available from our www pragmaticprogrammer.com website). But the best part is that you don't need a sweeping mandate to change your whole team or your whole company. You don't need to adopt Extreme Programming or Test-Driven Development, or change your development process in order to reap the proven benefits of unit testing. You can start unit testing, the pragmatic way, right away.
Table of Contents
About the Starter Kit Preface 1 Introduction 1.1 Coding With Confidence 1.2 What is Unit Testing? 1.3 Why Should I Bother with Unit Testing? 1.4 What Do I Want to Accomplish? 1.5 How Do I Do Unit Testing? 1.6 Excuses For Not Testing 1.7 Roadmap 2 Your First Unit Tests 2.1 Planning Tests 2.2 Testing a Simple Method. 2.3 More Tests 3 Writing Tests in JUnit 3.1 Structuring Unit Tests 3.2 JUnit Asserts 3.3 JUnit Framework 3.4 JUnit Test Composition 3.5 JUnit Custom Asserts 3.6 JUnit and Exceptions 3.7 More on Naming 3.8 JUnit Test Skeleton 4 What to Test: The Right-BICEP 4.1 Are the Results Right? 4.2 Boundary Conditions 4.3 Check Inverse Relationships 4.4 Cross-check Using Other Means 4.5 Force Error Conditions 4.6 Performance Characteristics 5 CORRECT Boundary Conditions 5.1 Conformance 5.2 Ordering 5.3 Range 5.4 Reference 5.5 Existence 5.6 Cardinality 5.7 Time 5.8 Try It Yourself 6 Using Mock Objects 6.1 Simple Stubs 6.2 Mock Objects 6.3 Testing a Servlet 6.4 Easy Mock Objects 7 Properties of Good Tests 7.1 Automatic 7.2 Thorough 7.3 Repeatable 7.4 Independent 7.5 Professional 7.6 Testing the Tests 8 Testing on a Project 8.1 Where to Put Test Code 8.2 Test Courtesy 8.3 Test Frequency 8.4 Tests and Legacy Code 8.5 Tests and Reviews 9 Design Issues 9.1 Designing for Testability 9.2 Refactoring for Testing 9.3 Testing the Class Invariant 9.4 Test-Driven Design 9.5 Testing Invalid Parameters A Gotchas A.1 As Long As The Code Works A.2 "Smoke" Tests A.3 "Works On My Machine" A.4 Floating-Point Problems A.5 Tests Take Too Long A.6 Tests Keep Breaking A.7 Tests Fail on Some Machines A.8 My main is Not Being Run B Installing JUnit B.1 Command-line installation B.2 Does it work? C JUnit Test Skeleton C.1 Helper Class C.2 Basic Template D Resources D.1 On The Web D.2 Bibliography E Summary: Pragmatic Unit Testing F Answers to Exercises
Andy Hunt and Dave Thomas have more than 50 years combined experience, developing software for clients around the world. For the last 10 years they've been working together as The Pragmatic Programmers, helping clients write software and improve their development processes. They are authors of the best-selling The Pragmatic Programmer, and have written several other books. They speak at conferences globally, and are editors of IEEE Software's "Construction" column.