#26 MockHttpServletRequest getSession(...) not creating new sess

closed
Alwin Ibba
None
5
2007-09-05
2007-09-05
shermand56
No

MockHttpServletRequest is not creating a new session in a circumstance where Tomcat's implementation is. The standard is a little unclear on the correct behavior, it could be read either way. I'd prefer the Mock objects act like a production implementation.

Specifically, with this sample code:
...
HttpSession sess = request.getSession();
String str = (String) sess.getAttribute("anAttribute");
sess.invalidate();
sess = request.getSession(true);
sess.setAttribute("anAttribute", str)
...

The unit test running Mockrunner's implementation fails, throwing java.lang.RuntimeException: session invalid at the setAttribute(...) call.

Running under tomcat (all versions I've tried) the code succeeds, the request creates a new session if it doesn't have a _valid_ one already.

I haven't tried other servlet containers, but this seems like reasonable behavior, to create a new session if you don't have a valid one already.

The specification at http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html#getSession\(boolean) is not quite explicit that getSession(boolean) should return a new session if the request doesn't have a valid one, but it could be read that way easily.

Discussion

  • Alwin Ibba
    Alwin Ibba
    2007-09-05

    • assigned_to: nobody --> aibba
     
  • Alwin Ibba
    Alwin Ibba
    2007-09-05

    • status: open --> closed
     
  • Alwin Ibba
    Alwin Ibba
    2007-09-05

    Logged In: YES
    user_id=804648
    Originator: NO

    Yes, you are right. This point has been discussed multiple times. I don't consider it a bug because that's the way the Mockrunner implementation works. There's only one session per test method and it's maintained by the MockObjectFactory. The mock request never creates new sessions. However it's on the refactoring list to make the behaviour closer to a real implementation.