In object-oriented programming, a mock object is a simulated object that mimics the behavior of the smallest testable parts of an application in controlled ways. A mock object makes use of the same interface as the element of code it is intended to imitate and is often used in unit testing to scrutinize the performance of an actual object. Mock objects help isolate the component being tested from the components it depends on and applying mock objects effectively is an important part of test-driven development (TDD).
A mock object can be useful in place of a real object that:
- Runs slowly or inefficiently in practical situations
- Occurs rarely and is difficult to produce artificially
- Produces non-deterministic results
- Does not yet exist in a practical sense
- Is intended mainly or exclusively for conducting tests.