aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 39547
    
  27
See here for a discussion on how to access private fields/methods using reflection.


Ping & DNS - updated with new look and Ping home screen widget
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
thanks Ulf and Sirisena.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing private methods using Reflection API
 
Similar Threads
Testing Private Methods????
JUnit - List of test methods through TestRunner
Serialization
Testing Private Methods
Private methods in TDD