Home » Unit Testing » Separation of interface from implementation
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.
As a result, the
software developer abstracts an interface around the database connection, and
then implements 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.