How can I have Java test private methods ?

478    Asked by himanshusingh in Java , Asked on Oct 12, 2022

I am working on a java project. I am new to unit testing. What is the best way to unit test private methods in java classes?

You generally don't unit Java test private methods directly. Since they are private, consider them an implementation detail. Nobody is ever going to call one of them and expect it to work a particular way.


You should instead test your public interface. If the methods that call your private methods are working as you expect, you then assume by extension that your private methods are working correctly.



Your Answer

Interviews

Parent Categories