Break It Down

Sometimes package access is not enough or is not available. Finding yourself unable to test tightly encapsulated functionality may indicate that your class is doing too much. Often you create private methods to simplify the implementation of the public interface. However, methods generally should be small. Your ability to functionally decompose a method into smaller methods of higher abstraction could mean that those methods constitute another class when taken together, particularly if they are reused.

Extracting your private implementation into a class can make it directly testable. Java allows more than one class in a file as long as the additional classes are not public; those classes have package scope. You can also make the ...

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.