4 Replies Latest reply on Apr 1, 2008 3:47 AM by pei yong

    NullPointer at JSFClientSession()

    Joe weiss Newbie

      Hi,

      I´m trying to setup jsfunit for my project and get the following error on my resultpage:

      it occures at the point where i´m making my first call in my TestSuite at this line:

      JSFClientSession client = new JSFClientSession("/app/initpage.jsf);
      ...


      here is the output:

      java.lang.NullPointerException
      at org.jboss.jsfunit.framework.FacesContextBridge.getCurrentInstance(FacesContextBridge.java:55)
      at org.jboss.jsfunit.facade.ClientIDs.(ClientIDs.java:55)
      at org.jboss.jsfunit.facade.JSFClientSession.doWebRequest(JSFClientSession.java:176)
      at org.jboss.jsfunit.facade.JSFClientSession.doInitialRequest(JSFClientSession.java:117)
      at org.jboss.jsfunit.facade.JSFClientSession.(JSFClientSession.java:70)


      Any Idea?

      Thanks,
      Joe

        • 1. Re: NullPointer at JSFClientSession()
          Stan Silvert Master

          Hi Joe,

          It's very strange that you would get a NullPointerException at that particular point.

          A couple of things to check:
          1) Did you update your web.xml with the JSFUnitFilter and add the proper mappings? See http://labs.jboss.com/jsfunit/gettingstarted.html

          2) Does your jboss-jsfunit-core-1.0-SNAPSHOT.jar contain faces-config.xml in the /meta-inf directory? Is this jar in /WEB-INF/lib or did you put it somewhere else?

          The reason I ask about faces-config.xml is that it contains the config to enable the JSFUnitFacesContextFactory. So whenever a FacesContext is created, it puts it in the HttpSession where JSFUnit can find it. With FacesContextBridge failing at line 55, it looks like it gets a handle to the HttpSession OK, but the FacesContext is not there.

          51 public static FacesContext getCurrentInstance()
          52 {
          53 HttpSession session = WebConversationFactory.getSessionFromThreadLocal();
          54 JSFUnitFacesContext facesContext = (JSFUnitFacesContext)session.getAttribute(JSFUnitFacesContext.SESSION_KEY);
          55 facesContext.setInstanceToJSFUnitThread();
          56 return facesContext;
          57 }


          To tell if the factory is not being initialized, you can put the declaration in your own faces-config.xml like this and see if it fixes the problem:
          <factory>
           <faces-context-factory>org.jboss.jsfunit.context.JSFUnitFacesContextFactory</faces-context-factory>
          </factory>


          Please let us know how this turned out.

          Regards,

          Stan

          • 2. Re: NullPointer at JSFClientSession()
            Joe weiss Newbie

            Thanks for your reply,

            the configuration settings you pointed out were all correct, but i found the solution. (at least i hope so)

            First of all i found out that my first page (login) was no jsf at all. So no JSF Context was built.
            So i processed this page with httpunit and started with my JSFClientSession after logging in.

            The same error occured, because the cookie you´re identifing the jsfunit request with was not set.

            so i used your WebConversationFactory to create my webcon., use it with httpunit, logging in, switching to JSFClient and it works. i´m not sure why, but well... ;)

            here is some code:

            public class JSFUnitTest extends org.apache.cactus.ServletTestCase
            {
            public static Test suite()
            {
            return new TestSuite( JSFUnitTest.class );
            }

            private void login() throws IOException, SAXException{
            WebConversation wc = WebConversationFactory.makeWebConversation();
            WebResponse resp = wc.getResponse( "http://localhost:8080/...." );
            WebForm form = resp.getFormWithID("login");
            form = resp.getFormWithName("login");
            form.setParameter("j_username", "user");
            form.setParameter("j_password", "pass");
            resp= form.submit();
            }

            public void testInitialPage() throws IOException, SAXException
            {
            login();

            // Send an HTTP request for the initial page
            JSFClientSession client = new JSFClientSession("/.../...");
            ...
            }
            }

            I´ll be back !! ;)

            Thanks,
            Joe

            • 3. Re: NullPointer at JSFClientSession()
              Stan Silvert Master

              OK. That makes sense. Yes, the initial page for JSFClientSession must be a JSF page. I'm thinking about how I can detect that and warn the developer if it is not.

              http://jira.jboss.com/jira/browse/JSFUNIT-44

              Looking at your new code, you should use the second constructor for JSFClientSession so that you pass in your newly created WebConversation. Otherwise, the session will be cleared.

              private WebConversation login() throws IOException, SAXException{
               WebConversation wc = WebConversationFactory.makeWebConversation();
               WebResponse resp = wc.getResponse( "http://localhost:8080/...." );
               WebForm form = resp.getFormWithID("login");
               form = resp.getFormWithName("login");
               form.setParameter("j_username", "user");
               form.setParameter("j_password", "pass");
               resp= form.submit();
               return wc;
              }
              
               public void testInitialPage() throws IOException, SAXException
              {
               WebConversation wc = login();
              
               // Send an HTTP request for the initial page
               JSFClientSession client = new JSFClientSession(wc, "/.../...");
              ...
              }


              Stan
              http://www.jsfunit.org

              • 4. Re: NullPointer at JSFClientSession()
                pei yong Newbie

                wonderful!

                peter