GeeCON Prague 2014*
The moose likes Java in General and the fly likes Testing private methods using Reflection API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Testing private methods using Reflection API" Watch "Testing private methods using Reflection API" New topic
Author

Testing private methods using Reflection API

praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
Ranchers,
I have tested private methods using reflection API.
Now I want to test a private method which inturens calls another private method.
Sure enough I get InvocationTargetException when I try to run this test.

for example,



in the above ex. i can run tests on privateMethodB(), but not on privateMethodA().
SO what do I do using reflection to test privateMethodA() ?

suggestions please...

Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Isn't it that your method is throwing an Exception? Did you call getCause on the InvocationTargetException to see what the wrapped Exception is?
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
the JUnit test throws an InvocationTargetException. this is because from the JUnit using reflection I call methodA. but since methodA calls methodB and that is private as well, an exception is thrown.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Have a look here

Call the getCause method and check the stack trace on the returned Throwable. It should give the cause as to why the InvocationTargetException was thrown.
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
the InvocationTargetException is thrown because i do not have permission to access privaMethodB when i am running private MehodA.
I wasnt o know how to avoid it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42039
    
  64
See here for a discussion on how to access private fields/methods using reflection.


Ping & DNS - my free Android networking tools app
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
thanks Ulf and Sirisena.
 
GeeCON Prague 2014
 
subject: Testing private methods using Reflection API