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.
No comments:
Post a Comment