473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to mock protected method in Java

intelrate
Here is a question.

I wanted my JUnit tests to be more modular and found how to mock protected methods 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.
Jun 11 '09 #1
4 4917
JosAH
11,448 Expert 8TB
Extend your class and override your protected method by a public one; test your extended class and your overriding public method.

kind regards,

Jos
Jun 11 '09 #2
Thank you JosAH

That's what I'm doing right now. The problem is that if my protected method has 2 arguments then my mock method should be like

Object mockMethod(Object parameter1, Object parameter2);

If I need to mock another protected method with 3 parameters then I have to create

Object mockMethod(Object parameter1, Object parameter2, Object parameter3);

It works for me but probably there is another approach to mock protected methods which I don't know about.

Cheers!
Jun 12 '09 #3
JosAH
11,448 Expert 8TB
@intelrate
Not that I know of but you shouldn't mock protected methods in the first place; you should mock the API of the class (the methods published in its interface).

kind regards,

Jos

ps. Was this just a 'pro forma' question? You already turned it into a blog entry ...
Jun 13 '09 #4
@JosAH
Actually I'm not totally happy with the solution I have but for now it should be fine.

Thanks.
Jun 13 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: wanadoo | last post by:
Hi There, Can anybody tell me where i can download free exams for Sun Certified Programmer for the Java 2 Platform 1.4? Thanks, Bill
0
by: John J. Lee | last post by:
I had the bright idea a week or two ago to construct mock objects (for unit testing) "by example" by calling methods on a generic mock object. Say we have a class Bar, and we want to check that,...
4
by: John J. Lee | last post by:
I'm trying define a class to act as a Mock "handler" object for testing urllib2.OpenerDirector. OpenerDirector (actually, my copy of it) does dir(handler.__class__) to find out what methods a...
1
by: Kutty Banerjee | last post by:
Hi, curious if there is a tool available for creating Mock Objects in C++ the JAVA style. kutty
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
2
by: Terry | last post by:
I'm looking at NMock2 as a framework to create mock objects during my unit testing. One part that I'm trying to understand is that it will mock interfaces, not concrete classes. I normally don't...
5
by: Pawel Pabich | last post by:
Hi, I need to mock Stream.Read method with RhinoMocks but it looks like it's impossible. Any ideas?
1
by: earthwormgaz | last post by:
Has anyone seen a tool for C++ that automatically creates mock classes? I found MockMaker for Java, but I wondered if there was something similar for us C++ developers. The idea is to read in...
0
by: Fuzzyman | last post by:
Mock 0.4.0 has just been released, the first release in about ten months (but worth the wait). Mock is a simple library for testing: specifically for mocking, stubbing and patching. * Mock...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.