Subscribe by Email


Friday, April 13, 2012

What is a mock object? What are the reasons for use?

Perhaps most of us have heard about mock objects but do not know what it is actually! This article is dedicated to the mock objects and the reasons for which they are used.

First we will clear up the concept of mock object and then we will move on to the reasons for its use. Mock objects are a concept of the object oriented programming.

" Mock objects are defined as some simulated objects that have the ability to mimic the behavior of the real objects in a defined and controlled way. Mock objects are used by a programmer or developer to check another object by testing it against the mock object."

Reasons for use of mock objects



- Mock objects as we discussed above are able to perfectly simulate the behaviour of real or we can say non- mock object be it very simple or very complex.

- This property of the mock objects make them an essential and convenient tool for carrying out an effective unit testing which involves testing of the software program modules in isolation.

- It should not be mistaken that the mock objects are used in the place of the original non mock objects.

- They are used in their place only during the testing phase and that too when it is almost impossible and impractical to incorporate the real object in to the created unit tests.

- In some of the cases it is required the presence of some object for testing another one. In such cases the mock objects play a very important role.

Characteristics of Mock Object


Below mentioned are some characteristics which if are present in a non mock object, it is important that you use a mock object in its place:

1. If the object supplies non deterministic results such as the current temperature and current time etc.

2. If the object is having states that are somewhat difficult to be reproduced and recreated such as a network error and so on.

3. If the object is too slow i.e., if it is having a complete data base which requires initializing before the starting of the test.

4. If the object does not exist in practical.

5. If the object tends to change its behavior.

6. If the object currently does not exists.

7. If the object requires including the methods and information regarding the testing purposes but not its actual tasks.

8. If the object is difficult to set up.

9. If the object is a user interface.

10. If the object cannot be queried.

We will give an example so that the mock object concept becomes clear to you!

- Consider your mobile alarm clock which causes an alarm bell to ring at the time that you specify.
- Suppose it gets the current time from somewhere outside, then how do you test this mobile alarm?
- You will have to wait till the alarm bell goes off till that specified time to check whether or not the mobile alarm is working correctly or not.
- Mock objects are sometimes used as mediators.
- In such cases a mock domain comes in to play rather than the actual domain.

Features of Mock Object


Below mentioned are some of the features of a mock object:
- Easy to create
- Easy to set up
- It is quick
- Deterministic
- Easy to cause behavior
- Does not have any direct user interface
- Can be queried directly

The mock objects did not come in to existence in a single day. It took a whole lot of days to experiment, discuss and collaborate and put forward the idea of using a mock object.


No comments:

Facebook activity