Ajay Kumar wrote:
>
> I have a controller servlet with many private methods and want to write
> a testcase for these methods. I tried using reflection for invoking
> these methods, but got the following report:
Using JUnit, I've always found it easier to refactor so that the
Servlet, in this case, delegates to a class with the bulk of the
functionality. JUnit uses exactly the same interface (in the general
sense) as the Servlet and everyone is happy. In any case it's probably
not a bad idea to arrange for classes that extend other groups' code to
be as small and simple as possible.
> <error message="null"
> type="org.apache.cactus.client.ServletExceptionWrapper">
> java.lang.reflect.InvocationTargetException
> [...]
> Caused by: java.lang.NullPointerException
> at
> com.appnetix.mainprospecting.control.MainServlet.getEmailTemplates(MainServlet.java:226)
That looks like your servlet throwing the NPE. Perhaps it's not
initialised? Method.invoke will itself throw an NPE if you try to call a
non-static method with a null target, althugh it doesn't look like the
case in this instance.
w.r.t security/accessibility, for private methods you should usually
call setAccessible(true) which is defined in Method's superclass,
otherwise an IllegalAccessException will be thrown. As ever you will
need suitably lax security, but Cactus requires that to be wide open anyway.
Tom Hawtin