Subscribe by Email


Showing posts with label Fake. Show all posts
Showing posts with label Fake. 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. 


Facebook activity