A mock object is a simulation of a real object. Mock objects act just as real objects but in a controlled way. A mock object is created to test the behavior of a real object. In unit testing, mock objects are used to scrutinize the performance of real objects. Simply said, a mock object is just the imitation of a real object. Some important characteristics of mock objects are that they are lightweight, easily created, quick and deterministic, and so on.
Stub ObjectA stub object is an object which implements an interface of a component. A stub can be configured to return a value as required. Stub objects provide a valid response, but it is of static nature meaning that no matter what input is passed in, we always get the same response.
Mock Object vs. Stub Object
Mock objects vary from stub objects in some ways. They are listed below:
The first distinct factor is that mock objects test themselves, i.e., they check if they are called at the proper time in the proper manner by the object being tested. Stubs generally just return stubbed data, which can be configured to change depending on how they are called.
Secondly, mock objects are generally lightweight relative to stubs with different instances set for mock objects for every test whilst stubs are often reused between tests.