Introduction

Unit testing is all about testing units of code. In object-oriented programming languages, a single unit test typically tests a single class. The best practice in unit testing is to test the public interfaces of a class. In rare circumstances, it may also be necessary to test implementation details of a class. This lesson will outline best practices and the techniques that are used to test class implementation details.

Identifying interface vs. implementation

An interface is those parts of code that specify inputs from and outputs to outside the class. The conversion of the input to the output is the responsibility of the implementation code. The class interface is the part that users of the class should be familiar with. The class implementation details are the part of the class that users of the class should not be concerned with.

A physical analogy for interface vs. implementation

Useful analogies of interface versus implementation are found in engineering. Consider a ball bearing as an example. The interface is those parts of the bearing that interact with the object in which it is housed. The most important interface elements are the inner diameter (ID) and outer diameter (OD).

Get hands-on with 1200+ tech skills courses.