Subscribe by Email


Showing posts with label Mock objects. Show all posts
Showing posts with label Mock objects. Show all posts

Monday, July 9, 2012

List out the technical details of mock object?


Mock objects are much in talk these days and seem to be marking themselves as one of the effective tools in carrying out unit testing. 

What are Mock Objects?


- Mock objects are simulated objects counterparts of the original or the non mock object that is intended to mimic the behaviour and working of the original non mock object in a way that is controlled and defined. 
- Classes with heavy code bases are such a pain to test and can be tested only by the mock objects. 
- Ever tried implementing unit tests in a project but failed to do so because of the dependency of the module and difficulty in its isolation?
- Within a budget and time constraint it becomes difficult for configuring and setting up all the external dependencies of the modules. 
- Mock objects are a feasible option for overcoming such problems. 
- Problem with most of the mock tools is that they do not come with documentation so it becomes difficult to work them out. 
- These technical details of mock objects are the center point of this article. 
- Since the mock objects simulate the real object, they are said to possess the same interface as that of the real objects that they are imitating. 
- Nowadays many mock object frame works are available using which the programmers and developers can specify which all methods are to be invoked by the mock object and in which order. 
- Also, the parameters that have to be passed to the mock object can be specified along with the values that will be returned by the mock object. 
- We have divided the technical details of the mock objects in to three categories:
1. Mocks, fakes and stubs
2. Setting expectations
3. Writing log strings

Mocks, fakes and stubs: 
- Some consider the mock objects and fakes to be different things saying that the fakes are much simpler than the mock objects. 
- The fakes are simply used to represent just the interface of the object which they are mimicking and responses that they return are pre-arranged. 
- Thus, we can say a set of method stubs is provided by the fake objects. 
- On the other hand, the mock objects are fake objects but they help in determining whether some other object passes or fails the test to which it is subjected. 
- It does so by the verification whether of the interaction if any took place on the object. 
Everything else apart from these two i.e., fake objects and mock objects is considered to be a stub. 
- Stub is anything unreal is fake and based up on its usage it can be classified as stubs and mocks. 
- Mock objects possess a little more functionality which is that their method implementations contain assertions that are truly theirs. 
- This furthermore implies that the context of each and every call is examined by the true mock object.
- The examination includes the checking of the order of calling of the methods and performing tests on the data that has been passed as arguments to the method calls.

Setting expectations: 
It is ensured by a mock setting that the calls to the system will cause the system to throw some exception like getting hanged or crashing.

Writing log strings: 
Mock methods can be used to make an entry to the public log strings which otherwise could result in invisible performance sapping bugs.


Monday, May 21, 2012

What is the use of a mock object in test driven development?


Over the years the idea of the mock object was materialized and today they are being used in numerous testing and development methodologies. Nowadays mock objects find their use in many testing methodologies like the unit testing and also in development processes such as the test driven development process. 
This article is focused up on the use of a mock object in the test driven development process. But, before moving further we shall give a brief discussion regarding the concepts of the mock object so that understanding the use of mock object in the test driven development becomes easy for you. 

Mock objects are known as the fake objects that in a way simulate the behaviour of an original or to say a non mock object whenever any of the following conditions are prevalent:
     
     1. If the non mock supplies non deterministic output.
     2. If the non mock object is difficult to be reproduced or recreated.
     3. If the non mock object is quite slow i.e., is using a complete data base.
     4. If the non mock object tends to change its behaviour.
     5. If the non mock object does not yet exists.
     6. If the non mock object demands information and methods exclusive to the testing purposes.

   

What are Mock Objects?


- Mock objects are nothing but fake objects but with a little more purpose of testing the other non mock objects with the method implementations having assertions of their own. 
- They are employed for the examination and verification of the context of each and every method call.
- Apart from this, they are also used to determine the order in which the methods are called and implemented using data that was passed as the arguments in the method calls.
Mock settings are an effective means for ensuring that an exception is generated by the software system or application when the subsequent calls to it are made like it may hang, fail or crash etc. 
With the mock settings, it has become possible to test the client behaviors against all the realistic faulty conditions as well as they can be developed also in the back end sub systems. 
- Without the use of mock objects it will be too difficult to test these conditions. 
- The mock system provides a simple and flexible way to test the realistic fault conditions since proper consideration is given to them.
- With the mock methods it has become easy to add entries to a public log string. 

Use of Mock Object in TDD


Now coming to the use of mock objects in the test driven development, mock objects are used in 
the test driven development during the writing of the software. 

- With the help of mock objects the programmers and developers are able to write and unit test functionality in a defined area by using the mock objects that provide all the interface requirements of the complex real objects rather than using the complex collaborating and underlying classes and objects. 
- With the help of mock objects the programmers are able to focus properly on the testing of the behavior of the SUT or system under test. 
- They don’t have worry about the objects on which it is dependent. 
- There are some practical speed issues also involved with the use of mock objects in the test driven development apart from the complexity issues.
- To develop a realistic piece of software employing the test driven development may require hundreds of unit tests. 
- If any communication is developed between these unit tests and the data bases, networked systems and web services, then it is possible that the suite of unit tests will become quite slow. 


Saturday, April 14, 2012

What are the advantages and limitations of a mock object?

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.


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.


Facebook activity