Do not test private methods

You should only be testing public methods. Private methods are an implementation detail of the object and are subject to heavy change, etc.  Any class, including test fixtures, that care about private methods on another object exhibit the “Inappropriate intimacy” code smell stench.

If you find the need to test a private method, then you’re doing something else wrong. There is an “upstream” problem, so to speak. You have arrived at this problem due to some other mistake previous in this process.  Try to isolate what that is, exactly, and remove that rather than bending your tests to go down a painful road of brittleness in testing.

Better yet, consider why you even need those private methods in the first place.  Why do you feel you need to test them? Is there some major functionality there worth testing? Maybe it shouldn’t be private?  Maybe you have a “responsibility violation” (a violation of the Single Responsibility Principle)?  Maybe you should break this functionality into another class?  There is no one right answer, it depends on your situation so judge accordingly.

In general, don’t let yourself fall into the “It’s legacy code, I can’t do anything” mental trap. There is usually something you can do. For example,  extract class and inject it, make the method public, etc.  If all else fails (and please try not to just give up here, give it a college try), then you can fall back to extraordinary means.

1 Year Anniversary