Mocking

Mock objects are simulated objects that mimic the behavior of real objects in controlled ways. (source)

In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. If an object has any of the following characteristics, it may be useful to use a mock object in its place:

  • the object supplies non-deterministic results (e.g. the current time);
  • it has states that are difficult to create or reproduce (e.g a network error);
  • it is slow (e.g. a complete database);
  • it does not yet exist or may change behavior;
  • it would have to include information and methods exclusively for testing purposes (and not for its actual task).