Introduction to Stub & Mock

Introduction to Stub & Mock


Posted in : Java Posted on : November 29, 2010 at 6:25 PM Comments : [ 0 ]

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)

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics