Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Short answer: Mockito’s standard API does not mock private methods. Usually, test the behavior through the public method that calls the helper. If a private method represents a separate responsibility, extract it behind a collaborator and mock that boundary instead. For a constrained legacy case, PowerMock documents private-method stubbing in Java; Telerik JustMock documents non-public member arrangements in C#, subject to edition and setup requirements.

Why private methods are difficult to mock

A private method is an implementation detail, not part of the class’s public contract. Mockito’s project wiki explains why its ordinary API does not mock private methods and recommends focusing tests on observable behavior or changing the design when private code needs its own testing seam: Mockito: Private Methods.

A test that stubs a private call is coupled to how the class currently works. A harmless internal refactor—renaming the helper, changing its arguments, or removing it—can break that test even if the public behavior remains correct.

Choose an approach based on what the test needs to prove

Test the public behavior when possible

Call the public method with representative inputs and assert its return value or externally visible effects. If the helper’s behavior matters, let it run as part of that test; the test need not call the private method directly to verify the class’s contract.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Extract a collaborator when the responsibility is independent

If the private helper performs work that has independent meaning—such as a distinct policy or interaction with an external dependency—consider moving that responsibility into a collaborator with a deliberate testable boundary. Test the collaborator separately and mock it through the framework’s normal APIs when testing the original class. Keep visibility aligned with the intended API; widening a method solely to satisfy a test can expose an implementation detail without creating a sound boundary.

Use private-method mocking only for a justified legacy constraint

If refactoring has substantial cost and a legacy test must isolate a private call, a specialized framework may provide an escape hatch. Treat such tests as implementation-coupled, and check the framework’s compatibility with your Java or .NET runtime, test runner, and other mocking dependencies before adopting an example.

Java: PowerMock’s documented private-method approach

PowerMock documents private partial mocking with a spy. Its historical wiki example prepares the class for testing, creates a PowerMockito.spy, stubs the private call with PowerMockito.doReturn(value).when(spy, "methodToMock", args), and verifies it with PowerMockito.verifyPrivate(spy, times(n)).invoke("methodToMock", args): PowerMock: Mocking Private Method.

This is a framework-specific pattern, not a current, universal setup recipe. The wiki is historical and its dependency guidance should not be treated as a recommendation for current versions. Check the exact PowerMock, Mockito, JUnit or other runner, and Java versions in your project, along with the required class preparation and test-runner configuration. Do not assume that copying an old example into a modern project will work unchanged.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

PowerMock itself cautions that its advanced features require expertise: “PowerMock is mainly intended for people with expert knowledge in unit testing. Putting it in the hands of junior developers may cause more harm than good.” See the PowerMock project documentation.

C#: JustMock’s documented non-public arrangements

Telerik documents arranging non-public members in C# with Mock.NonPublic.Arrange(target, "MethodName", args). Its documentation says this feature is available only in the commercial version and requires elevated mode: JustMock: Mocking Non-Public Members. Verify the current product requirements and API against the JustMock version and runtime used by your project; this is not a general capability of every .NET mocking library.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to decide

  • The public result or effects are what matter: invoke the public method and assert those outcomes.
  • The helper owns an independent responsibility: extract it into a collaborator and test or mock that boundary.
  • You cannot reasonably refactor a legacy case: consider a compatible private-mocking tool, document its versions and setup, and accept that the test may need changes when the implementation changes.
  • You are considering making the method public or internal just for the test: do so only if that visibility expresses an intentional design boundary, not merely to work around the test.

Framework support varies by language, visibility, method type and overload, and can depend on the runtime, bytecode or class-loader behavior, and test runner. The cited Java and C# examples illustrate particular tools; they are not a complete comparison of available frameworks.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.