This section contains the Introduction of Stub & Mock
Introduction to Stub & Mock
-
Consider a situation, where you have a class Calculator, that needs a dao (Data Access Object) object to load the data it needs from a database. Setting up the connection and inserting the data in the database can be a lot of work.
-
Instead you can provide the Calculator instance with a fake dao class which just returns the data you need for the test. The fake dao class will not actually read the data from the database. The fake dao class is a mock object or stub object.
-
Mock testing means unit testing with mock objects as substitutes for real objects. Here, real object is dao class.
-
The situation of the Calculator class using a dao object can be generalized to "a unit that uses a collaborator". Here unit is Calculator class and collaborator is dao class. We can represent it symbolically as :
uses
unit-------------->collaborator
-
When the collaborator is exchanged with a mock (or stub), it would be expressed like this :
uses
unit-------------->mock(or stub)
[ 0 ] Comments