CSC/ECE 517 Fall 2009/wiki 1a2rd

From Expertiza_Wiki
Revision as of 03:34, 17 September 2009 by Rpandit (talk | contribs) (→‎ See Also)
Jump to navigation Jump to search

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. 

 

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 summarizes the concept 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.


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.

 

LIMITATIONS (There have to be some ?)

  1. Who will guard the guard?
    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?
    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?
    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://www.onjava.com/pub/a/onjava/2004/02/11/mocks.html Approaches to Mocking | O'Reilly Media.
2.http://en.wikipedia.org/wiki/Mock_object Wiki Mock Objects.
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.