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
posted
0
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: 64
posted
0
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.
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: 64
posted
0
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: 35258
7
posted
0
See here for a discussion on how to access private fields/methods using reflection.