Monday, July 18, 2005

Mock-ing objects for Unit Tests

Mock objects are being used extensively in Unit Testing. In-fact there is a Mock framework that can be downloaded and added as a reference which helps in some of the functionalities needed.
Why do we need these mock objects? Take the example where you would need to test your code against a database access. The database is not created. But for your test to run you would need to create the whole database immediately which is a waste of time and resources for the time being. Therefore it is better to use a Mock database which provides the necessary information without going real. This task is achieved by creating interfaces which are implemented by the real database class as well as by the mock database class. Thereby the pointer to the interface is used which is cast appropriately to the correct type and hence you get to use the mock database for the class.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home