Mock objects are nowadays quite popular with the testers. The mock objects are now being used widely for conducting unit testing and also in many software development processes like the test driven development.
- The mock objects are known for their mimicking behaviour of the other non mock or original objects for which they have been created.
- The mock objects are used under the context of the object oriented programming.
- They are nothing but the fake objects that are simulated in a way as to mimic the behavior of the real objects in a way that is properly controlled and defined.
- The behavior of most of the non mock or real objects is tested against the behavior of a mock object.
- You can compare the mock object with a crash test dummy that is used by the car designers to the impact of the vehicle on the human body.
Reasons for Use
Few of them have been mentioned below:
1. The real object provides non deterministic result like current temperature and time.
2. The real object has the states that cannot be easily recreated or reproduced such as a network error.
3. The real object is quite slow such as a data base that needs initializing before the commencing of the test.
4. The real object does not exist currently.
5. The real object tends to change its behaviour.
6. The real object is to include the information for the testing purposes.
Advantages of Mock Object
- One of the characteristic of the mock objects is that they possess the same interface as that of the real objects that they mimic.
- With such an arrangement the client stays unaware of whether he is using the real object or the mock object.
- With the use of mock objects it becomes easy for the programmer to specify which and all methods will be invoked and in what order and it can also be specified which parameters are to be passed to the mock objects.
- All this is done by using the mock object frame works.
- The mock objects can even mimic the behaviour of the complex objects, for example the network socket.
- The use of mock objects can help the programmer in various ways.
- The programmer can determine whether the object that is under test responds well to the variety of situations it is put in works properly or not.
Limitations of Mock Object
- Use of mock objects can lead to close coupling of the several unit tests similar to that in the actual implementation of the code that is under testing.
- If a programmer is using the mock object frame work, the order of and number of times the mock methods are to be invoked on a mock object can be specified.
- If the code is subsequently refactored, it may fail even though the contract of the previous implementation is followed in the method.
- The unit tests are known to test only the external behaviour of the mock methods rather than testing its internal implementation.
- Therefore, maintenance needed for the tests increases if there is an overuse of the mock objects.
- Mock objects need to model the real objects perfectly well which becomes difficult if the real object has not been written yet.
No comments:
Post a Comment