Testing Private method using mockito
The correct approach is to target the exposed behavior through public APIs to test a private method. Indicate reflection as a last resort, not first:
The public interface that leverages the private method should be the core of your focus. Apply reflection judiciously and cautiously.
Coping with objects & collaborations
When testing, ensure that you weigh up the association between objects more than the individual private methods. Vital and effective testing usually involves the assessment of interactions and outcomes. Instead of adjusting your private methods' access modifiers, which, let's admit, wrecks encapsulation, refactor the code to be more welcoming to testability.
Calling in the big guns: Powermock & Whitebox
In desperate and dire need of testing a private method? The PowerMock library - your savior, can be brought into the equation as it integrates well with Mockito. Powermock's Whitebox
and MockPrivate
features come to the rescue and test private methods:
Here, Whitebox
swings in to mutate private fields optimally before invoking private methods:
Remain cautious though! PowerMock has ties with explicit test runners and versions of the Java Development Kit (JDK).
Spy, Stub, and Reflection
To reinforce your testing strategy, spy objects and reflection come into play when you need to verify how a private method interacts with others:
The above example checks the call to the private method within the flow of a public method without invoking the private logic.
The public behavior is the Hollywood Star
Instead of constantly being fixated on the private method itself, aim your lens at the public behavior that it triggers. Examine the public methods, their various impacts, and consequences. By doing this, you're making sure that the class's external contract is well-respected and the private methods fit right in with the public API.
Design remodeling for testability
Consider these pointers to tailor your classes for testability. Hidden business logic can be:
- Extracted to a new class
- Switched to "package-private" for easy testing
- The focusing lens should primarily be on public methods
Reflection: the use & misuse
Carefully weigh the benefits against the drawbacks of apparent tactics like ReflectionTestUtils
. The booby traps may lure you into fragility pitfalls and threaten the test suite's stability.
Was this article helpful?