Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to mock protected method in Java

807588Jun 11 2009 — edited Jun 13 2009
Here is a question.

I wanted my JUnit tests to be more modular and found [how to mock protected methods|http://intelrate.blogspot.com/2009/05/how-to-mock-protected-method-in-java.html] for that purpose. Briefly speaking, we cannot mock and control calls of protected method with JDK dynamic proxy because protected methods are not in any interface but we can create MockInterface with mock method under control and call it from overridden protected method to achieve our goal.

The problem I’m trying to resolve now is that my mock method should have the same number of parameters as protected method I want to mock. It forces me to create several mock methods with different number of parameters which is not elegant. Does anyone know better solution in JMock or other Java framework? Any help would be very appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 11 2009
Added on Jun 11 2009
16 comments
3,239 views