Use Available Facilities

This is frequently expressed as “test through the interface.” In the general sense, the statements are equivalent, but object-oriented programming languages have also given us a narrower interpretation that is not sufficient. This principle is broader than simply using the methods declared in an interface.

When writing tests, we want to minimize the degree to which we change the code under test to accommodate testability. The best way to do that is to use the features that already exist. This may be obvious for constructors, setters, and getters and completely natural and necessary for method arguments, return values, and other direct functionality. I have often seen other features of the code overlooked as potential ...

Get Quality Code: Software Testing Principles, Practices, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.