Test-Only Interfaces

You can mitigate the exposure of changing access levels. You can introduce test-only interfaces to combine access level change with a weaker form of breaking down your class. In general, it enhances extensibility and testability to use interfaces as the primary types rather than concrete classes. From an object-oriented design perspective, this separates your definition of the behavioral specification from its implementation. Also, Java’s mocking frameworks work best with interfaces, although they are getting better at handling concrete classes.

By using interfaces as the primary types, callers can only access the methods defined in that interface unless they have the bad manners to use the implementation class directly. ...

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.