|
JUnit Recipes Practical Methods for Programmer Testing J. B. Rainsberger with contributions by Scott Stirling 2004 | 752 pages ISBN: 1932394230 |
|||
![]() |
$49.95 | Softbound print book | |
![]() |
$25.00 | PDF ebook | |
Preface
PrefaceIf you have ever met me, either online or in person, then perhaps you have heard me tell this story.
I was working on a large project at the IBM labs in Toronto. It was in the middle of the year 2000, long after the Y2K craze had ended, and I had spent nearly three months working on a component scheduled for delivery in about one month. The defects were coming in steadily from our testing department, and each fix was just another patch on top of an increasingly disgusting Big Ball of Mud. It was around that time that I read a draft of Extreme Programming Installed, by Ron Jeffries, Ann Anderson, and Chet Hendrickson. With the help of the Internet, this draft led me to www.junit.org, where I learned about this great new tool for testing Java code, called JUnit. Within minutes I knew this would help my cause.
Soon after this, I marched into my manager's office and announced that there was no way I would be able to patch the existing code to fix the remaining defects in time to deliver. The code had become too complicated to understand. I could not predict how many new defects I would inject while fixing the ones we knew about. We simply were not getting feedback quickly enough. "Send me home," I told him, "and let me write it all again from scratch. I'll use JUnit to test it as I go. It will work." He did. When it came down to it, what choice did he have?
Even before I knew how to use JUnit effectively, I rewrote three months' worth of code in nine long days, with JUnit by my side. What had originally taken well over 500 hours of effort and didn't work had been rebuilt in about 100 hours, including a suite of over 125 tests. That was enough for me—I was hooked on JUnit.
Since that time I have involved myself in the JUnit community, not only as a practitioner of Test-Driven Development, but also by answering questions at the JUnit Yahoo! group. Over the years I have refined my JUnit technique to the point where I am certain I could write that same component in closer to 25 hours of work. The ability to eliminate 95% of the time needed for any task is significant; and while there's no way to prove it, I attribute the majority of that savings to JUnit.
In 2001 I read a number of complaints about the JUnit documentation. Apparently there were no suitable tutorials. I decided to write "JUnit: A Starter Guide," a tutorial which still draws over 1000 readers monthly. That tutorial was the genesis of this book, even though I didn't know it at the time. Much of this book's content has been refined from the answers I have provided to questions on the JUnit mailing lists. Still more came from hard-won personal experience using JUnit on various projects. I wanted this book to describe how I use JUnit; I did not want it to present some idealized view of how one ought to use it. There's already too much opinion and waving of hands out there—but not in here. This book contains JUnit techniques that work, because they have made my projects successful. For that reason it is worth noting two things: much of what I present here is my opinion, backed up by my experience; and this is not the only way to do it. This book contains recommendations—not rules.
By the time this book is printed and in your hands, things will have changed. Some of these recipes might be obsolete. There is not much I can do about that—people are discovering great new ways to use JUnit every day. Even if a few of these recipes become dated, the concepts - the motivations behind the recipes—never change. Test isolation is important. Smaller tests are more powerful. Separating the implementation from the interface makes testing easier. Decoupling your code from the framework makes testing possible. Watch for these recurring themes throughout. They are the most valuable part of the book, because they will help you long after all of us stop writing software in Java, whenever that happens. If you find them useful, then I have done my job as an author and as a member of the JUnit community.
DESCRIPTION
When testing becomes a developer's habit good things tend to happen--good productivity, good code, and good job satisfaction. If you want some of that, there's no better way to start your testing habit, nor to continue feeding it, than with JUnit Recipes. In this book you will find one hundred and thirty seven solutions to a range of problems, from simple to complex, selected for you by an experienced developer and master tester. Each recipe follows the same organization giving you the problem and its background before discussing your options in solving it.
JUnit—the unit testing framework for Java—is simple to use, but some code can be tricky to test. When you're facing such code you will be glad to have this book. It is a how-to reference full of practical advice on all issues of testing, from how to name your test case classes to how to test complicated J2EE applications. Its valuable advice includes side matters that can have a big payoff, like how to organize your test data or how to manage expensive test resources.
What's Inside
- Getting started with JUnit
- Recipes for
- servlets
- JSPs
- EJBs
- Database code
- much more
- Difficult-to-test designs, and how to fix them
- How testing saves time
- Choose a JUnit extension:
- HTMLUnit
- XMLUnit
- ServletUnit
- EasyMock
- and more!
ABOUT THE AUTHORS...
J. B. Rainsberger is a developer and consultant who has been a leader in the JUnit community since 2001. His popular online tutorial JUnit: A Starter Guide is read by thousands of new JUnit users each month. Joe lives in Toronto, Canada.
Scott Stirling is a Senior Software Engineer on the Platform and Tools team at Workscape, Inc. in Framingham, MA. He has been active in the JUnit
community since 2000 and has contributed code to the Jakarta Ant

