Java Reference
In-Depth Information
In most cases, writing mock object tests has a nice side effect: it forces you to
rewrite some of the code under test. In practice, code is often not written well. You
hardcode unnecessary couplings between the classes and the environment. It's easy to
write code that's hard to reuse in a different context, and a little nudge can have a big
effect on other classes in the system (similar to the domino effect). With mock objects,
you must think differently about the code and apply better design patterns, like inter-
faces and Inversion of Control.
Mock objects should be viewed not only as a unit testing technique but also as a
design technique. A new rising star among methodologies called test-driven develop-
ment ( TDD ) advocates writing tests before writing code. With TDD , you don't have to
refactor your code to enable unit testing: the code is already under test! (For a full
treatment of the TDD approach, see Kent Beck's book Test Driven Development: By Exam-
ple . 6 For a brief introduction, see chapter 5.)
Although writing mock objects is easy, it can become tiresome when you need to
mock hundreds of objects. In the following chapters, we present several open source
frameworks that automatically generate ready-to-use mocks for your classes, making it
a pleasure to use the mock objects strategy.
6
Kent Beck, Test Driven Development: By Example (Boston: Addison-Wesley, 2003).
 
Search WWH ::




Custom Search