-
1. Re: SessionId obtained when running FacesRequest test is null
marcioendo.marcioendo.gmail.com Apr 3, 2009 9:53 PM (in response to atorble)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 Apr 6, 2009 4:30 PM (in response to 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 Apr 6, 2009 4:33 PM (in response to atorble)Oops! Slight cut n paste typo there. The precedence of MockSession should of course be MOCK.