Video in progress
Should you test private methods separately? Justify your view.
Usually no: a private method is tested through the public methods, because it's an implementation detail. If testing a private method on its own feels really necessary, it's often logic that should be moved into a separate class.
📖 Answer
You can test a private method by calling it with reflection, but that nails the test to the internal structure of the code. You rename the method or split it in two, and the test breaks even though the behavior didn't change.
Testing through the public API gives you freedom to refactor: rewrite the inside however you like, and if the tests stay green, everything is fine.
But here's a real case: OrderService has a 60-line private calculateDiscount with dozens of conditions, and checking it through placeOrder means building a whole order every time. That's a signal: the logic deserves its own DiscountPolicy class. You extract it, make it public, and write unit tests freely.
There's a middle path too: make the method package-private and mark it with Guava's @VisibleForTesting annotation. It's a quick fix, but if you use it a lot, class boundaries get loose.
In an interview, answering "it's a signal about the design" will look stronger than saying "I do it with reflection".
Sign up to mark studied questions and save your progress.