Gaurav
Gaurav

Reputation: 1650

JMock unable to mock a private method

I am trying to write code to test a method by mocking the class in JMock, this method internally calls three different methods(one of them is a private method). I set up the expectations for all the three internal methods. Then I call method, which is under test. The test case fails saying that it expected the private method to be called and it was not called. What is happening is that the two internal methods(with default access specifier) are mocked successfully but the private method is not mocked and in fact the control reaches inside the actual private method causing the problem. When I change the access specifier of the private method to default access, then everything works fine, but I don't want to do that. Can somebody please explain this behavior and let me know how to resolve this problem

Upvotes: 1

Views: 2624

Answers (2)

hellodear
hellodear

Reputation: 226

You can use Jmockit also. It's an excellent library you can use for this use case. Keyword you can use: "Deencapsulation" .

Upvotes: 0

ZeroOne
ZeroOne

Reputation: 3171

JMock cannot mock private methods, plain and simple. Mockito cannot mock private methods either. If you really want to mock private methods, you need to use the PowerMock framework.

Upvotes: 1

Related Questions