Should Private/Protected methods be under unit test? [closed]

No, I don’t think of testing private or protected methods. The private and protected methods of a class aren’t part of the public interface, so they don’t expose public behavior. Generally these methods are created by refactorings you apply after you’ve made your test turn green.

So these private methods are tested implicitly by the tests that assert the behavior of your public interface.

On a more philosophical note, remember that you’re testing behavior, not methods. So if you think of the set of things that the class under test can do, as long as you can test and assert that the class behaves as expected, whether there are private (and protected) methods that are used internally by the class to implement that behavior is irrelevant. Those methods are implementation details of the public behavior.

Leave a Comment