CSC/ECE 517 Fall 2009/wiki 1a2rd: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(53 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Mock Objects are simulated objects in the object oriented environment to mimic the behavior of real objects in a controlled fashion. This comes in very handy when testing an application which interacts with a complex object. The controlled simulation facilitates the testing process which otherwise would be difficult of the behavior is unpredictable.
Mock Objects are simulated objects in the object oriented environment to mimic the behavior of real objects in a controlled fashion. This comes in very handy when testing an application which interacts with a complex object. The controlled simulation facilitates the testing process which otherwise would be difficult if the behavior is unpredictable.[[http://www.mockobjects.com 4]],[3]


 
 
 
=== BACKGROUND -(And why do we need it?)===
=== And why do we need it?? ===
 Mock objects mimic the behavior of complex real objects in a controlled (user defined) way and thus are highly useful in unit testing when the real objects are impractical or impossible to be incorporated in the unit tests. If the real object has any of the following characteristic, it may be useful to use mock objects instead:
 Mock Objects mimic the behavior of complex real objects in a controlled (user defined) way and thus are highly useful in unit testing when the real objects are impractical or impossible to be incorporated in the unit tests. If the real object has any of the following characteristic, it may be useful to use mock objects instead:


# Has non deterministic behavior (results, time taken to respond etc..)
# Has non deterministic behavior (results, time taken to respond etc..)
Line 14: Line 13:
 
 


=== What’s the fuss about? ===
=== INTRODUCTION (What’s the fuss about?) ===
Wikipedia provides a good introduction to Mock Objects  
Wikipedia provides a good introduction to Mock Objects  


"In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A computer programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts."
"''In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A computer programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts.''"[[http://en.wikipedia.org/wiki/Mock_object 1]]


This introduction summarizes the concept of mock objects in a non-technical but in a complete manner.
This introduction captures the essence of mock objects in a non-technical but in a complete manner.


 
 


=== Under the hood. ===
=== DETAILS (Under the hood.) ===
The mock object is created to mimic the behavior of a real object. This is achieved by having the interface of the mock objects same as that of the real objects. This causes the application under the test to be ignorant of whether it is interfacing with a real object or a mock object.
The mock object is created to mimic the behavior of a real object. This is achieved by having the interface of the mock objects same as that of the real objects. This causes the application under the test to be ignorant of whether it is interfacing with a real object or a mock object.[[http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html 2]], [[http://en.wikipedia.org/wiki/Mock_object 1]]
 
 
Computer scientists also draw a line of differentiation between mocks and fakes. If an object merely implements the interface and responds with predetermined responses it is considered as fake where as the mock objects which internally takes into the consideration the state of the object that is being mocked is termed as mock. Consider for example if we want to mock a file system. A fake would respond with standard responses, that means it is possible to read a file without opening it (remember the responses are standard), where as a real mock has to ensure that a file is opened first and then read method is invoked. This can be achieved by writing down some assertions in the mock objects.[[http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html 2]], [[http://en.wikipedia.org/wiki/Mock_object 1]]




Computer Scientists also draw a line of differentiation between mocks and fakes. If an object merely implements the interface and responds with predetermined responses it is considered as fake where as the mock objects which internally takes into the consideration the state of the object that is being mocked is termed as mock. Consider for example if we want to mock a file system. A fake would respond with standard responses, that means it is possible to read a file without opening it (remember the responses are standard), where as a real mock has to ensure that a file is opened first and then read method is invoked. This can be achieved by writing down some assertions in the mock objects.
Similar to assertions Mock objects can have expectations that are set by the unit test to facilitate simulation of a condition in the real objects. Consider testing a online credit card transaction software. The authorization usually is a third party process is mocked. In case the tester intends to test the transaction procedure when the card is not authorized then the mock object can have a field authorization expectation which can be set by the unit test as fail and pass accordingly testing the different scenarios.[[http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html 2]], [[http://en.wikipedia.org/wiki/Mock_object 1]]


 
 


Similar to assertions Mock objects can have expectations that are set by the unit test to facilitate simulation of a condition in the real objects. Consider testing a online credit card transaction software. The authorization usually is a third party process is mocked. In case the tester intends to test the transaction procedure when the card is not authorized then the mock object can have a field authorization expectation which can be set by the unit test as fail and pass accordingly testing the different scenarios.
=== LIMITATIONS (There have to be some ?) ===
 
# Who will guard the guard?[[http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html 2]]<br>Mock Objects are used for unit-testing, but who will ensure that the mock object is correctly mimicking the behavior of a real object. Care needs to be taken while implementing mock objects, if the behavior is improperly implemented then it will cause the test cases to fail even when they should pass or even worse may cause failing test cases to pass.
# Excess of everything is bad. Isn’t it?[[http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html 2]]<br>The over use of mock objects in a unit test may result in yielding little value to testing. If everything is mocked then there is a high chance that the application under test might not be adequately tested.
# Poor use of Mock?[[http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html 2]]<br>The close coupling of a mock object with a unit test might result in very high maintenance cost for the mock object itself when the requirement for application under test changes and subsequently test case changes. Ideally mock objects should be concerned with behavioral aspects of real objects and the coupling with test cases should be minimal if not none.


&nbsp;
&nbsp;


=== There have to be some limitations. ===
===  A comparison of available Mock Object generation frameworks ===
# Who will guard the guard?&nbsp; <br/> Mock Objects are used for unit-testing, but who will ensure that the mock object is correctly mimicking the behavior of a real object. Care needs to be taken while implementing mock objects, if the behavior is improperly implemented then it will cause the test cases to fail even when they should pass or even worse may cause failing test cases to pass.
 
# Excess of everything is bad. Isn’t it???The over use of mock objects in a unit test may result in yielding little value to testing. If everything is mocked then there is a high chance that the application under test might not be adequately tested.
The table below gives a general comparison of some of the available mock object generating frameworks.
# Poor use of Mock?The close coupling of a mock object with a unit test might result in very high maintenance cost for the mock object itself when the requirement for application under test changes and subsequently test case changes. Ideally mock objects should be concerned with behavioral aspects of real objects and the coupling with test cases should be minimal if not none.


&nbsp;


===  Available mock object frameworks ===


{| class="prettytable"
{| class="wikitable" style="text-align:center;" border="1"
| <center>'''Name'''</center>
| <center>'''Name'''</center>
| <center>'''Supporting Language'''</center>
| <center>'''Supporting Language'''</center>
Line 52: Line 55:
|-
|-
| <center>[http://cmock.sourceforge.net/ CMock]</center>
| <center>[http://cmock.sourceforge.net/ CMock]</center>
| == [http://en.wikipedia.org/wiki/C_%28programming_language%29 C] ==
| [http://en.wikipedia.org/wiki/C_%28programming_language%29 C]  


| [http://cmock.sourceforge.net/ CMock] is a ruby-based tool and it helps to automate creation of mock functions for unit testing in the C language. This tool requires Ruby's Make-like facility and generate C code automatically [http://cmock.sourceforge.net/ CMock].  
| [http://cmock.sourceforge.net/ CMock] is a ruby-based tool and it helps to automate creation of mock functions for unit testing in the C language. This tool requires Ruby's Make-like facility and generate C code automatically [http://cmock.sourceforge.net/ CMock].  
| <center>[http://sourceforge.net/softwaremap/trove_list.php?form_cat=188 MIT License]</center>
| <center>[http://sourceforge.net/softwaremap/trove_list.php?form_cat=188 MIT License]</center>
|-
|
|
|
|


|-
|-
| <center>[http://mockpp.sf.net/ mockpp]</center>
| <center>[http://mockpp.sf.net/ mockpp]</center>
| == [http://en.wikipedia.org/wiki/C%2B%2B C++] ==
| [http://en.wikipedia.org/wiki/C%2B%2B C++]  


| [http://mockpp.sf.net/ mockpp] parses the header file of C++ (, which is converted into XML format) and created various user defined C++ code to emulate some functionality of your existing code rely on.
| [http://mockpp.sf.net/ mockpp] parses the header file of C++ (, which is converted into XML format) and created various user defined C++ code to emulate some functionality of your existing code rely on.
| <center>[http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public ][http://www.gnu.org/copyleft/lesser.html License][http://www.gnu.org/copyleft/lesser.html  ]</center>
| <center>[http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License]</center>




Line 75: Line 72:
|-
|-
| <center>[http://code.google.com/p/amop amop]</center>
| <center>[http://code.google.com/p/amop amop]</center>
| == [http://en.wikipedia.org/wiki/C%2B%2B C++] ==
| [http://en.wikipedia.org/wiki/C%2B%2B C++]  


| [http://code.google.com/p/amop amop] uses ABI and template techniques and then, simulate a pseudo-"Reflection". This one does not require the interfaces of the generated mock object.
| [http://code.google.com/p/amop amop] uses ABI and template techniques and then, simulate a pseudo-"Reflection". This one does not require the interfaces of the generated mock object.
Line 85: Line 82:
|-
|-
| <center>[http://code.google.com/p/mockitnow MockItNow]</center>
| <center>[http://code.google.com/p/mockitnow MockItNow]</center>
| == [http://en.wikipedia.org/wiki/C%2B%2B C++] ==
| [http://en.wikipedia.org/wiki/C%2B%2B C++]  


| [http://code.google.com/p/mockitnow MockItNow] captures function calls and reorder them to record and replay.  
| [http://code.google.com/p/mockitnow MockItNow] captures function calls and reorder them to record and replay.  
| <center>[http://www.opensource.org/licenses/mit-license.php MIT License][http://www.apache.org/licenses/  ]</center>
| <center>[http://www.opensource.org/licenses/mit-license.php MIT License]</center>


|-
|-
| <center>[http://code.google.com/p/m0cxx0r/ m0cxx0r]</center>
| <center>[http://code.google.com/p/m0cxx0r/ m0cxx0r]</center>
| == [http://en.wikipedia.org/wiki/C%2B%2B C++] ==
| [http://en.wikipedia.org/wiki/C%2B%2B C++]  


| [http://code.google.com/p/m0cxx0r/ m0cxx0r] records function calls and compares them to expectation. Then, the tool generates mock objects for C++.
| [http://code.google.com/p/m0cxx0r/ m0cxx0r] records function calls and compares them to expectation. Then, the tool generates mock objects for C++.
| <center>[http://www.opensource.org/licenses/bsd-license.php New BSD License] </center>
| <center>[http://www.opensource.org/licenses/bsd-license.php New BSD License] </center>
|-
|
|
|
|


|-
|-
| <center>[http://www.codeplex.com/attach Attach]</center>
| <center>[http://www.codeplex.com/attach Attach]</center>
| == [http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29 C#] ==
| [http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29 C#]  


| [http://www.codeplex.com/attach Attach] dynamically generates stub objects with support of the the 3A pattern (arrange/act/assert). This tool does not require interfaces.
| [http://www.codeplex.com/attach Attach] dynamically generates stub objects with support of the 3A pattern (arrange/act/assert). This tool does not require interfaces.
| <center>[http://www.opensource.org/licenses/ms-pl.html Microsoft Public License ]</center>
| <center>[http://www.opensource.org/licenses/ms-pl.html Microsoft Public License ]</center>


|-
|-
| <center>[http://code.google.com/p/moq/ Moq]</center>
| <center>[http://code.google.com/p/moq/ Moq]</center>
| == [http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29 C#] ==
| [http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29 C#]  


| [http://code.google.com/p/moq/ Moq] is requires setting up common expectations to a fixture setup method and generates .NET mock objects.
| [http://code.google.com/p/moq/ Moq] requires setting up common expectations using a fixture setup method and generates .NET mock objects.




Line 122: Line 113:
|-
|-
| <center>[http://nmock.org/ NMock and NMock 2]</center>
| <center>[http://nmock.org/ NMock and NMock 2]</center>
| == [http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29 C#] ==
| [http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29 C#]  


| This tool is a dynamic [http://www.mockobjects.com/ mock object] library for C#.
| This tool is a dynamic [http://www.mockobjects.com/ mock object] library for C#.
Line 134: Line 125:
|-
|-
| <center>[http://sourceforge.net/projects/mocklib NMockLib]</center>
| <center>[http://sourceforge.net/projects/mocklib NMockLib]</center>
| == [http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29 C#] ==
| [http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29 C#]  


| [http://sourceforge.net/projects/mocklib NMockLib] is simulates entire apis of code and interfaces for C#.
| [http://sourceforge.net/projects/mocklib NMockLib] simulates entire APIs of code and interfaces for C#.
| <center>[http://sourceforge.net/softwaremap/trove_list.php?form_cat=401 Apache 2 License]</center>
| <center>[http://sourceforge.net/softwaremap/trove_list.php?form_cat=401 Apache 2 License]</center>
|-
|
|
|
|
|-
| colspan="4" |


|-
|-
| <center>[http://www.easymock.org/ EasyMock]</center>
| <center>[http://www.easymock.org/ EasyMock]</center>
| == [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] ==
| [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java]  


| [http://www.easymock.org/ EasyMock] records expected behavior on given test code and replay them at runtime.
| [http://www.easymock.org/ EasyMock] records expected behavior on given test code and replay them at runtime.
Line 157: Line 139:
|-
|-
| <center>[http://www.jmock.org/ JMock]</center>
| <center>[http://www.jmock.org/ JMock]</center>
| == [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] ==
| [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java]  


| [http://www.jmock.org/ JMock] requires definition of mock objects and generates mock object at runtime.
| [http://www.jmock.org/ JMock] requires definition of mock objects and generates mock object at runtime.
Line 164: Line 146:
|-
|-
| <center>[http://mockmaker.sourceforge.net/ MockMaker]</center>
| <center>[http://mockmaker.sourceforge.net/ MockMaker]</center>
| == [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java] ==
| [http://en.wikipedia.org/wiki/Java_%28programming_language%29 Java]  


| [http://mockmaker.sourceforge.net/ MockMaker] requires user defined interface definitions at compile-time (build time)
| [http://mockmaker.sourceforge.net/ MockMaker] requires user defined interface definitions at compile-time (build time)
| <center>[http://mockmaker.sourceforge.net/license.html MockMaker License]</center>
| <center>[http://mockmaker.sourceforge.net/license.html MockMaker License]</center>
|-
|
|
|
|


|-
|-
| <center>[http://jsmock.sourceforge.net/ JSMock]</center>
| <center>[http://jsmock.sourceforge.net/ JSMock]</center>
| == [http://en.wikipedia.org/wiki/JavaScript JavaScript] ==
| [http://en.wikipedia.org/wiki/JavaScript JavaScript]  


| [http://jsmock.sourceforge.net/ JSMock] uses expectation recording and matching techniques. The tool generates stub on object method calls.  
| [http://jsmock.sourceforge.net/ JSMock] uses expectation recording and matching techniques. The tool generates stub on object method calls.  
Line 184: Line 160:
|-
|-
| <center>[http://code.google.com/p/jsmocktool/ JSMockTool]</center>
| <center>[http://code.google.com/p/jsmocktool/ JSMockTool]</center>
| == [http://en.wikipedia.org/wiki/JavaScript JavaScript] ==
| [http://en.wikipedia.org/wiki/JavaScript JavaScript]  


| [http://code.google.com/p/jsmocktool/ JSMockTool] is a Javascript Mock Tool is Mock library using RSpec style.  
| [http://code.google.com/p/jsmocktool/ JSMockTool] is a JavaScript Mock Tool is Mock library using [http://rspec.rubyforge.org/ RSpec] style.  
| <center>[http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License] </center>
| <center>[http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License] </center>


|-
|-
| <center>[http://mock4js.sourceforge.net/ Mock4JS]</center>
| <center>[http://mock4js.sourceforge.net/ Mock4JS]</center>
| == [http://en.wikipedia.org/wiki/JavaScript JavaScript] ==
| [http://en.wikipedia.org/wiki/JavaScript JavaScript]  


| [http://mock4js.sourceforge.net/ Mock4JS] is a dynamic-mock library for Javascript
| [http://mock4js.sourceforge.net/ Mock4JS] is a dynamic-mock library for JavaScript
| <center>[http://sourceforge.net/softwaremap/trove_list.php?form_cat=188 MIT License] </center>
| <center>[http://sourceforge.net/softwaremap/trove_list.php?form_cat=188 MIT License] </center>
|-
|
|
|
|


|-
|-
| <center>[http://search.cpan.org/dist/Test-MockObject/lib/Test/MockObject.pm Test::MockObject]</center>
| <center>[http://search.cpan.org/dist/Test-MockObject/lib/Test/MockObject.pm Test::MockObject]</center>
| == [http://en.wikipedia.org/wiki/Perl Perl] ==
| [http://en.wikipedia.org/wiki/Perl Perl]  


| [http://search.cpan.org/dist/Test-MockObject/lib/Test/MockObject.pm Test::MockObject] helps you to create mock objects that conform to given interfaces for Perl.
| [http://search.cpan.org/dist/Test-MockObject/lib/Test/MockObject.pm Test::MockObject] helps you to create mock objects that conform to given interfaces for Perl.
| <center>[http://www.ohloh.net/licenses/10 Artistic License] and [http://www.ohloh.net/licenses/3 GNU General Public License 2.0] </center>
| <center>[http://www.ohloh.net/licenses/10 Artistic License] and [http://www.ohloh.net/licenses/3 GNU General Public ][http://www.ohloh.net/licenses/3 License 2.0] </center>


|-
|-
| <center>[http://search.cpan.org/perldoc?Test::Mock::Class Test::Mock::Class]</center>
| <center>[http://search.cpan.org/perldoc?Test::Mock::Class Test::Mock::Class]</center>
| == [http://en.wikipedia.org/wiki/Perl Perl] ==
| [http://en.wikipedia.org/wiki/Perl Perl]  


| [http://search.cpan.org/perldoc?Test::Mock::Class Test::Mock::Class] creates mock classes based on [http://search.cpan.org/perldoc?Moose Moose] and [http://search.cpan.org/perldoc?Class%3A%3AMOP Class::MOP] for Perl.
| [http://search.cpan.org/perldoc?Test::Mock::Class Test::Mock::Class] creates mock classes based on [http://search.cpan.org/perldoc?Moose Moose] and [http://search.cpan.org/perldoc?Class%3A%3AMOP Class::MOP] for Perl.
| <center>[http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License]</center>
| <center>[http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License]</center>
|-
|
|
|
|


|-
|-
| <center>[http://mocha.rubyforge.org/ Mocha]</center>
| <center>[http://mocha.rubyforge.org/ Mocha]</center>
| == [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby] ==
| [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby]  


| [http://mocha.rubyforge.org/ Mocha] is a library to help generates mock and stub objects for Ruby using a similar syntax of [http://www.jmock.org/ JMock].  
| [http://mocha.rubyforge.org/ Mocha] is a library to help generates mock and stub objects for Ruby using a similar syntax of [http://www.jmock.org/ JMock].  
Line 231: Line 195:
|-
|-
| <center>[http://rspec.rubyforge.org/ RSpec]</center>
| <center>[http://rspec.rubyforge.org/ RSpec]</center>
| == [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby] ==
| [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby]  


| is the original behavior driven development framework and generates mock objects for Ruby  
| [http://rspec.rubyforge.org/ RSpec] is the original behavior driven development framework and generates mock objects for Ruby  
| <center>[http://rubyforge.org/softwaremap/trove_list.php?form_cat=188 MIT/X Consortium License] </center>
| <center>[http://rubyforge.org/softwaremap/trove_list.php?form_cat=188 MIT/X Consortium License] </center>


|-
|-
| <center>[http://flexmock.rubyforge.org/ FlexMock]</center>
| <center>[http://flexmock.rubyforge.org/ FlexMock]</center>
| == [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby] ==
| [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby]  


| [http://flexmock.rubyforge.org/ FlexMock] uses [http://rspec.rubyforge.org/ RSpec] behavior specification and generates mock objects for Ruby.
| [http://flexmock.rubyforge.org/ FlexMock] uses [http://rspec.rubyforge.org/ RSpec] behavior specification and generates mock objects for Ruby.
Line 244: Line 208:


|}
|}
&nbsp;
===  See Also ===
* [http://en.wikipedia.org/wiki/Mock_object Wikipedia- Mock Objects]
* [http://en.wikipedia.org/wiki/List_of_mock_object_frameworks Wikipedia- List of Mock Object Generation Frameworks]
* [http://www.mockobjects.com/ A blog on Mock Objects]
* [http://code.google.com/p/googlemock/ Homepage of Google C++ Mocking Framework]
&nbsp;
===  References ===
1.[http://en.wikipedia.org/wiki/Mock_object http://en.wikipedia.org/wiki/Mock_object] Wiki Mock Objects.
2.[http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html] Approaches to Mocking | O'Reilly Media.
3.T.Mackinnon, S. Freeman, and P.Craig, Endo-testing: Unit Testing with Mock Objects.
  In proceedings of International Conference on eXtreme programming and flexible processes in Software Engineering(XP),
  pages 287-301,2000/
4.[http://www.mockobjects.com http://www.mockobjects.com] A blog on mock objects.
&nbsp;&nbsp;

Latest revision as of 04:01, 17 September 2009

Mock Objects are simulated objects in the object oriented environment to mimic the behavior of real objects in a controlled fashion. This comes in very handy when testing an application which interacts with a complex object. The controlled simulation facilitates the testing process which otherwise would be difficult if the behavior is unpredictable.[4],[3]

 

BACKGROUND -(And why do we need it?)

 Mock objects mimic the behavior of complex real objects in a controlled (user defined) way and thus are highly useful in unit testing when the real objects are impractical or impossible to be incorporated in the unit tests. If the real object has any of the following characteristic, it may be useful to use mock objects instead:

  1. Has non deterministic behavior (results, time taken to respond etc..)
  2. Has internal states that are difficult to reproduce
  3. Is slow as compared to rest of application under test (example Databases)
  4. Does not exists (e.g., interfacing module still in development or the object might change behavior)
  5. Collaborates with the application under test exclusively for the test-cases.

 

INTRODUCTION (What’s the fuss about?)

Wikipedia provides a good introduction to Mock Objects

"In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A computer programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts."[1]

This introduction captures the essence of mock objects in a non-technical but in a complete manner.

 

DETAILS (Under the hood.)

The mock object is created to mimic the behavior of a real object. This is achieved by having the interface of the mock objects same as that of the real objects. This causes the application under the test to be ignorant of whether it is interfacing with a real object or a mock object.[2], [1]


Computer scientists also draw a line of differentiation between mocks and fakes. If an object merely implements the interface and responds with predetermined responses it is considered as fake where as the mock objects which internally takes into the consideration the state of the object that is being mocked is termed as mock. Consider for example if we want to mock a file system. A fake would respond with standard responses, that means it is possible to read a file without opening it (remember the responses are standard), where as a real mock has to ensure that a file is opened first and then read method is invoked. This can be achieved by writing down some assertions in the mock objects.[2], [1]


Similar to assertions Mock objects can have expectations that are set by the unit test to facilitate simulation of a condition in the real objects. Consider testing a online credit card transaction software. The authorization usually is a third party process is mocked. In case the tester intends to test the transaction procedure when the card is not authorized then the mock object can have a field authorization expectation which can be set by the unit test as fail and pass accordingly testing the different scenarios.[2], [1]

 

LIMITATIONS (There have to be some ?)

  1. Who will guard the guard?[2]
    Mock Objects are used for unit-testing, but who will ensure that the mock object is correctly mimicking the behavior of a real object. Care needs to be taken while implementing mock objects, if the behavior is improperly implemented then it will cause the test cases to fail even when they should pass or even worse may cause failing test cases to pass.
  2. Excess of everything is bad. Isn’t it?[2]
    The over use of mock objects in a unit test may result in yielding little value to testing. If everything is mocked then there is a high chance that the application under test might not be adequately tested.
  3. Poor use of Mock?[2]
    The close coupling of a mock object with a unit test might result in very high maintenance cost for the mock object itself when the requirement for application under test changes and subsequently test case changes. Ideally mock objects should be concerned with behavioral aspects of real objects and the coupling with test cases should be minimal if not none.

 

 A comparison of available Mock Object generation frameworks

The table below gives a general comparison of some of the available mock object generating frameworks.


Name
Supporting Language
Feature
License
CMock
C CMock is a ruby-based tool and it helps to automate creation of mock functions for unit testing in the C language. This tool requires Ruby's Make-like facility and generate C code automatically CMock.
MIT License
mockpp
C++ mockpp parses the header file of C++ (, which is converted into XML format) and created various user defined C++ code to emulate some functionality of your existing code rely on.
GNU Lesser General Public License



amop
C++ amop uses ABI and template techniques and then, simulate a pseudo-"Reflection". This one does not require the interfaces of the generated mock object.


MIT License
MockItNow
C++ MockItNow captures function calls and reorder them to record and replay.
MIT License
m0cxx0r
C++ m0cxx0r records function calls and compares them to expectation. Then, the tool generates mock objects for C++.
New BSD License
Attach
C# Attach dynamically generates stub objects with support of the 3A pattern (arrange/act/assert). This tool does not require interfaces.
Microsoft Public License
Moq
C# Moq requires setting up common expectations using a fixture setup method and generates .NET mock objects.


New BSD License
NMock and NMock 2
C# This tool is a dynamic mock object library for C#.
Nmock License
 



NMockLib
C# NMockLib simulates entire APIs of code and interfaces for C#.
Apache 2 License
EasyMock
Java EasyMock records expected behavior on given test code and replay them at runtime.
Apache 2 License
JMock
Java JMock requires definition of mock objects and generates mock object at runtime.
jMock License
MockMaker
Java MockMaker requires user defined interface definitions at compile-time (build time)
MockMaker License
JSMock
JavaScript JSMock uses expectation recording and matching techniques. The tool generates stub on object method calls.
GNU Library or Lesser General Public License
JSMockTool
JavaScript JSMockTool is a JavaScript Mock Tool is Mock library using RSpec style.
GNU Lesser General Public License
Mock4JS
JavaScript Mock4JS is a dynamic-mock library for JavaScript
MIT License
Test::MockObject
Perl Test::MockObject helps you to create mock objects that conform to given interfaces for Perl.
Artistic License and GNU General Public License 2.0
Test::Mock::Class
Perl Test::Mock::Class creates mock classes based on Moose and Class::MOP for Perl.
GNU Lesser General Public License
Mocha
Ruby Mocha is a library to help generates mock and stub objects for Ruby using a similar syntax of JMock.
MIT License
RSpec
Ruby RSpec is the original behavior driven development framework and generates mock objects for Ruby
MIT/X Consortium License
FlexMock
Ruby FlexMock uses RSpec behavior specification and generates mock objects for Ruby.
FlexMock License

 

 See Also

 

 References

1.http://en.wikipedia.org/wiki/Mock_object Wiki Mock Objects.
2.http://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html Approaches to Mocking | O'Reilly Media.
3.T.Mackinnon, S. Freeman, and P.Craig, Endo-testing: Unit Testing with Mock Objects. 
  In proceedings of International Conference on eXtreme programming and flexible processes in Software Engineering(XP), 
  pages 287-301,2000/
4.http://www.mockobjects.com A blog on mock objects.