This extract from the book “Pragmatic Unit Testing in Java with JUnit” written by Andy Hunt and Dave Thomas provides an introduction to the usage of mock objects during Java unit tests. They define the usage of mocks in unit testing as the unit similar to the use of lighting doubles in the movies: A mock object is simply a debug replacement for a real-world
The three steps to using mock objects in unit testing are:
1. Use an interface to describe the object
2. Implement the interface for production code
3. Implement the interface in a mock object for testing
The article provides code examples on how to write and use mock objects in a Java context. It also introduces EasyMock, an open source tool that provides Mock Objects for interfaces (and objects through the class extension) by generating them on the fly using Java’s proxy mechanism.