JSFUnit and Spring-Security problem
wumbrath Dec 17, 2008 5:57 AMHello,
we have a JSF web app using spring security for authentication. We wrote a JSFUnit test for testing the authentication. Following the (pretty good) documentation on the website the test looks like:
WebClientSpec wcSpec = new WebClientSpec("/index.jsf"); FormAuthenticationStrategy formAuth = new FormAuthenticationStrategy("username", "password"); formAuth.setSubmitComponent("login"); wcSpec.setInitialRequestStrategy(formAuth); JSFSession jsfSession = new JSFSession(wcSpec); JSFClientSession client = jsfSession.getJSFClientSession(); JSFServerSession server = jsfSession.getJSFServerSession();
The submitted login form calls the following piece of code
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()) .getRequestDispatcher("/j_spring_security_check"); dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse()); FacesContext.getCurrentInstance().responseComplete();
This works fine when using the web app through the browser.
The problem is that the test case throws a NullPointerException when trying to call FacesContext.getCurrentInstance() in the last line of the code posting. The user is authenticated successfully but it seems to me that either the forwarding or the session invalidation performed by spring during the authentication process causes JSFUnit to throw the Exception.
The test output is
testLogin(my.tests.jsfunit.JSFUnitTest) Time elapsed: 0.859 sec <<< ERROR! java.lang.IllegalStateException at org.mortbay.jetty.servlet.AbstractSessionManager$Session.getAttribute(AbstractSessionManager.java:784) at org.jboss.jsfunit.framework.FacesContextBridge.getCurrentInstance(FacesContextBridge.java:55) at org.jboss.jsfunit.jsfsession.JSFServerSession.pageCreated(JSFServerSession.java:172) at org.jboss.jsfunit.jsfsession.JSFServerSession.<init>(JSFServerSession.java:54) at org.jboss.jsfunit.jsfsession.JSFSession.<init>(JSFSession.java:84) at de.dailab.spree.prototypes.ph.jsfunit.JSFUnitTest.testLogin(JSFUnitTest.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at junit.framework.TestCase.runTest(TestCase.java:168) at junit.framework.TestCase.runBare(TestCase.java:134) at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153) at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119) at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93) at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:224) at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java) at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101)
Is there a possibility to test the authentication? I could not find a solution in the docs/faq/forum so far.
If you need any more information please tell me.
Thank you in advance