What is a Mock Object?
A mock object is generally used as a stand-in, or replacement, for a real object while the system is being unit tested. The main purpose of using a mock object is to allow our tests to be more focused, self contained, predictable and reliable. By having our unit tests more isolated and predictable we can identify the cause of the failure in less time and false negatives (tests which fail but should pass) are reduced. An example of when a mock object might be used is when a feature in your system is required to return different results based on the current time of day. Given this requirement, how could it be tested? It would have a number of problems, for example you would have to have different test logic depending on the time of day the tests where executed and worse, only being able to execute the test at certain times of the day in order for it to execute the conditional logic being tested. Identifying the real reason why a test has failed would also become a problem as it could be
Mock objects in the world of computer programming are simulations that can be used to measure performance and response in a controlled environment. The main function of the mock object is to interact with specific components of hardware and software within an experiment. By engaging a mock object within a simulation, it is possible to test the response of the component within a wide range of scenarios, and possibly enhance the response of the component before release to the general public. Unit testing with the use of a mock object is common to just about every computer programmer and manufacturer. The idea behind the mock object is to get real time information about response and performance in a lab situation. By creating a number of different situations that are likely to be encountered in the real world, the programmer can effectively qualify a final release of some type of hardware or software program. This use of mock objects to work out glitches in the equipment or software progr
Object Mocking is used to keep dependencies out of your unit test. Sometimes you’ll have a test like “SelectPerson” which will select a person from the database and return a Person object. To do this, you would normally need a dependency on the database, however with object mocking you can simulate the interaction with the database with a mock framework, so it might return a dataset which looks like one returned from the database and you can then test your code to ensure that it handles translating a dataset to a person object, rather than using it to test that a connection to the database exists.
Unit testing is the testing of software units in isolation. However, most units do not work alone, but they collaborate with other units. To test a unit in isolation, we have to simulate the collaborators in the test. A Mock Object is a test-oriented replacement for a collaborator. It is configured to simulate the object that it replaces in a simple way. In contrast to a stub, a Mock Object also verifies whether it is used as expected. Tim Mackinnon, Steve Freeman and Philip Craig pioneered the concept of Mock Objects, and coined the term. They presented it at the XP2000 conference in their paper Endo Testing: Unit Testing with Mock Objects. A reworked version of the paper is published in the book Extreme Programming Examined (Addison-Wesley). EasyMock has been the first dynamic Mock Object generator, relieving users of hand-writing Mock Objects, or generating code for them. It was presented for the first time at OOPSLA 2001.