3 Replies Latest reply on Apr 6, 2009 4:33 PM by atorble

    SessionId obtained when running FacesRequest test is null

    atorble

      Hi All,


      I have a test that exercises some code in a session scoped bean that does some manipulation based on the logged in user's sessionId.  The snippet of code being tested looks like this:



      ExternalContext externalContext = facesContext.getExternalContext();
      HttpSession _session = (HttpSession)externalContext.getSession(true;
      


      where facesContext is injected.


      When the app is run in JBoss this returns a sensible value but when I call the action method from a test class (invokeApplication() from within a FacesRequest instance running in jboss-embedded) it always returns null which breaks my test.


      Can anyone suggest how I might resolve this?


      TIA


      seam 2.0.2 SP1


      Andy

        • 1. Re: SessionId obtained when running FacesRequest test is null
          marcioendo.marcioendo.gmail.com

          I assume you are trying to inject org.jboss.seam.faces.FacesContext, correct?


          Check your logs and see if it is getting installed.


          Otherwise I'd suspect your missing some jars in your test classpath, most probably jsf-api.jar.

          • 2. Re: SessionId obtained when running FacesRequest test is null
            atorble

            Correct on both counts.


            For the possible benefit of others this is what I did in the end.


            A bit of digging around revealed that the HttpSession being served by SeamTest was a mock object that always returned null for getId().  So I decided to create a Seam component named httpSession in SESSION scope and used that in my app to replace the code to derive the session id from the facesContext component.  The class itself is just a thin wrapper round a 'real' HttpSession


            @Name("httpSession")
            @Scope(SESSION)
            @Install(precedence = APPLICATION)
            @Startup
            @BypassInterceptors
            public class Session {
                @Unwrap
                public javax.servlet.http.HttpSession getSession() {
                  return (HttpSession)  FacesContext.getCurrentInstance().getExternalContext().getSession(true);
                }
            }
            



            I also created a MockHttpSession class in the src/test area that replaces the above component during testing and provides my code with a mock sessionId.


            @Name("httpSession")
            @Scope(SESSION)
            @Install(precedence = APPLICATION)
            @Startup
            @BypassInterceptors
            public class MockSession extends MockHttpSession {
            
              private static final String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
            
              public MockSession() {
                super(null);
              }
            
              @Override
              public String getId() {
                StringBuffer sb = new StringBuffer(20);
                Random rand = new Random();
                for (int i = 0; i < 20; i++) {
                  sb.append(chars.charAt(rand.nextInt(35);
                }
                return sb.toString();
              }
            }
            



            Works like a dream :-)

            • 3. Re: SessionId obtained when running FacesRequest test is null
              atorble

              Oops! Slight cut n paste typo there.  The precedence of MockSession should of course be MOCK.