
Pragmatic Unit Testing: In Java with JUnit / Edition 1
by Andy Hunt, Dave ThomasISBN-10: 0974514012
ISBN-13: 9780974514017
Pub. Date: 09/28/2003
Publisher: Pragmatic Programmers, LLC, The
Learn how to improve your Java coding skills using unit testing. Despite it's 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
Overview
Learn how to improve your Java coding skills using unit testing. Despite it's 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 testthe 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
Product Details
- ISBN-13:
- 9780974514017
- Publisher:
- Pragmatic Programmers, LLC, The
- Publication date:
- 09/28/2003
- Series:
- Pragmatic Programmers Series
- Edition description:
- New Edition
- Pages:
- 176
- Product dimensions:
- 7.48(w) x 8.94(h) x 0.59(d)
Table of Contents
About the Starter Kit | xi | |
Preface | xiii | |
1 | Introduction | 1 |
1.1 | Coding With Confidence | 2 |
1.2 | What is Unit Testing? | 3 |
1.3 | Why Should I Bother with Unit Testing? | 4 |
1.4 | What Do I Want to Accomplish? | 5 |
1.5 | How Do I Do Unit Testing? | 7 |
1.6 | Excuses For Not Testing | 7 |
1.7 | Roadmap | 12 |
2 | Your First Unit Tests | 13 |
2.1 | Planning Tests | 14 |
2.2 | Testing a Simple Method | 15 |
2.3 | More Tests | 20 |
3 | Writing Tests in JUnit | 21 |
3.1 | Structuring Unit Tests | 21 |
3.2 | JUnit Asserts | 22 |
3.3 | JUnit Framework | 26 |
3.4 | JUnit Test Composition | 27 |
3.5 | JUnit Custom Asserts | 32 |
3.6 | JUnit and Exceptions | 33 |
3.7 | More on Naming | 35 |
3.8 | JUnit Test Skeleton | 35 |
4 | What to Test: The Right-BICEP | 37 |
4.1 | Are the Results Right? | 38 |
4.2 | Boundary Conditions | 41 |
4.3 | Check Inverse Relationships | 42 |
4.4 | Cross-check Using Other Means | 42 |
4.5 | Force Error Conditions | 43 |
4.6 | Performance Characteristics | 44 |
5 | Correct Boundary Conditions | 47 |
5.1 | Conformance | 48 |
5.2 | Ordering | 49 |
5.3 | Range | 51 |
5.4 | Reference | 54 |
5.5 | Existence | 55 |
5.6 | Cardinality | 56 |
5.7 | Time | 58 |
5.8 | Try It Yourself | 60 |
6 | Using Mock Objects | 65 |
6.1 | Simple Stubs | 66 |
6.2 | Mock Objects | 67 |
6.3 | Testing a Servlet | 71 |
6.4 | Easy Mock Objects | 74 |
7 | Properties of Good Tests | 79 |
7.1 | Automatic | 80 |
7.2 | Thorough | 81 |
7.3 | Repeatable | 83 |
7.4 | Independent | 83 |
7.5 | Professional | 84 |
7.6 | Testing the Tests | 86 |
8 | Testing on a Project | 89 |
8.1 | Where to Put Test Code | 89 |
8.2 | Test Courtesy | 93 |
8.3 | Test Frequency | 94 |
8.4 | Tests and Legacy Code | 95 |
8.5 | Tests and Reviews | 98 |
9 | Design Issues | 101 |
9.1 | Designing for Testability | 101 |
9.2 | Refactoring for Testing | 103 |
9.3 | Testing the Class Invariant | 114 |
9.4 | Test-Driven Design | 117 |
9.5 | Testing Invalid Parameters | 119 |
A | Gotchas | 121 |
A.1 | As Long As The Code Works | 121 |
A.2 | "Smoke" Tests | 121 |
A.3 | "Works On My Machine" | 122 |
A.4 | Floating-Point Problems | 122 |
A.5 | Tests Take Too Long | 123 |
A.6 | Tests Keep Breaking | 123 |
A.7 | Tests Fail on Some Machines | 124 |
A.8 | My main is Not Being Run | 125 |
B | Installing JUnit | 127 |
B.1 | Command-line installation | 128 |
B.2 | Does it work? | 129 |
C | JUnit Test Skeleton | 131 |
C.1 | Helper Class | 133 |
C.2 | Basic Template | 133 |
D | Resources | 137 |
D.1 | On The Web | 137 |
D.2 | Bibliography | 139 |
E | Summary: Pragmatic Unit Testing | 141 |
F | Answers to Exercises | 143 |
Customer Reviews
Average Review:
Most Helpful Customer Reviews
![]() |
Unit testing is `by the developer for the developer¿, it¿s a tool used to ensure code written withstand the tests of time through changes in requirements, bug fixes, and added functionality. Unit (leveraging JUnit) tests are written in Java (there is also a C# version of this text) that live within a testing framework called JUnit and are executed against our application code on a periodic basis (whether that be hourly, daily or at deployment, its up to you) to validate that the code under test continues to perform as per its requirements throughout the project lifecycle. This way we write code that conforms to the `ilities¿, reliability, repeatability, maintainability, and scalability. The book takes you through a nicely sculpted path of learning from the initial `baby-steps¿ Unit testing examples to the more complex day to day challenges of writing loosely coupled Unit tests (using MockObjects) in surrounding environments that include things such as the network, database, or even a Servlet Container. And while winding its way down this path we are also introduced to pearls of wisdom in Chapters titled, `What to Test¿, `Using MockObjects¿, `Properties of Good Tests¿, `Testing on a Project¿, `Design Issues¿, and `Gotchas¿. And finally at the end of the path or text we find a list of very useful testing resources, the text summary, and answers to all exercise questions in the book. This is a definite must have for the programmers tool-kit.
|