DEV Community 👩‍💻👨‍💻

Discussion on: Do you unit test private methods?

renegadecoder94 profile image
Jeremy Grifski

Hey! I'm late to the party, but I was just in the process of writing an article on this exact topic when I found yours.

From what I've read, most people prefer to test the public API and ignore private methods altogether (sort of). In other words, they exercise the underlying private methods through the public interface through white box testing.

Of course, I'm a lot less against private function/method testing than most people. As with many debates in tech, I think there's always an edge case where it makes sense. For example, I was recently working on a utility class in Java that only had one public method: main. Do I go through the effort of trying to mock up the streams and feed in data, or does it make more sense to test the underlying structure before performing some sort of integration test? I opted for the latter, but to each their own.