Sunday, June 7, 2009

Separation of interface from implementation

Because some classes may have references to other classes, testing a class can frequently spill over into testing another class. A common example of this is classes that depend on a database: in order to test the class, the tester often writes code that interacts with the database.


This is a mistake, because a unit test should never go outside of its own class boundary instead, the software developer should create an abstract interface around the database connection, and then implement that interface with their own mock object.

By abstracting this necessary attachment from the code (temporarily reducing the net effective coupling), the independent unit can be more thoroughly tested than may have been previously achieved.

This results in a higher quality unit that is also more maintainable.

No comments:

Post a Comment