You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Avinash Chugh <av...@hotmail.com> on 2002/11/21 13:57:25 UTC

Error accessing same http session across test methods

Environment: Weblogic 7, Win2k, Cactus 1.4.1

I have a ServletTestCase with two test methods - testLogin(), testLogout().

testlogin calls a doPost() on a servlet which logs in user using HTTP 
request parameters, and places an object in session. When I test 
assertNotNull for that session attribute, the test succeeds.

testlogout() calls another doPost() which logs out the earlier user and 
invalidate the session. But when I do assertNotNull() for the same 
session attribute (before the actual call to servlet), the test fails..

I am using the same servlet...and session.isNew() always returns true

This leads me to believe..
The HTTPSession created by testLogin() can nolonger be accessed by 
subsequent test method(s) ??

This is a bug..right ??

Regards,


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Error accessing same http session across test methods

Posted by Vincent Massol <vm...@octo.com>.
Hi Avinash,

Nick is right: Cactus creates a new HTTP Session for each testXXX().
Unless you specify not to create one
(WebRequest.setAutomaticSession(false)).

That said, there are cases when you need to be able to unit test methods
that expect an HTTP Session to exist. A method that tests for
Session.isNew() is such a case...

In order to be able to test this, Cactus 1.5 (it is not present in
Cactus 1.4) has introduced a new method:

WebRequest.getSessionCookie()

See
http://cvs.apache.org/viewcvs/jakarta-cactus/documentation/docs/xdocs/ch
anges.xml?rev=1.57&content-type=text/vnd.viewcvs-markup (search for
"HTTP Session")

Here is the test that is included in the Cactus test suite:

public void beginCreateSessionCookie(WebRequest theRequest)
{
    HttpSessionCookie sessionCookie = theRequest.getSessionCookie();
    theRequest.addCookie(sessionCookie);
}

public void testCreateSessionCookie()
{
    assertTrue("A session should have been created prior to "
        + "this request", !session.isNew());
}

You need to get a nightly build
(http://cvs.apache.org/builds/jakarta-cactus/nightly/2002-11-16/).

Cheers,
-Vincent

> -----Original Message-----
> From: Avinash Chugh [mailto:avi_chugh@hotmail.com]
> Sent: 21 November 2002 12:57
> To: cactus-user@jakarta.apache.org
> Subject: Error accessing same http session across test methods
> 
> Environment: Weblogic 7, Win2k, Cactus 1.4.1
> 
> I have a ServletTestCase with two test methods - testLogin(),
> testLogout().
> 
> testlogin calls a doPost() on a servlet which logs in user using HTTP
> request parameters, and places an object in session. When I test
> assertNotNull for that session attribute, the test succeeds.
> 
> testlogout() calls another doPost() which logs out the earlier user
and
> invalidate the session. But when I do assertNotNull() for the same
> session attribute (before the actual call to servlet), the test
fails..
> 
> I am using the same servlet...and session.isNew() always returns true
> 
> This leads me to believe..
> The HTTPSession created by testLogin() can nolonger be accessed by
> subsequent test method(s) ??
> 
> This is a bug..right ??
> 
> Regards,
> 
> 
> --
> To unsubscribe, e-mail:   <mailto:cactus-user-
> unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:cactus-user-
> help@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Error accessing same http session across test methods

Posted by Nicholas Lesiecki <ni...@eblox.com>.
No, it's as designed. Each test method gets a new session. Not only that,
but test methods are not guaranteed to operate in any sequence, so relying
on state from one method to the next is generally a bad idea.

See:
http://jakarta.apache.org/cactus/faq.html#faq4


Cheers,
Nicholas Lesiecki
Principal Software Engineer
eBlox, Inc.
(520) 615-9345 x104
Check out my book!:
Java Tools for Extreme Programming:

http://www.amazon.com/exec/obidos/ASIN/047120708X/

Check out my articles on AspectJ:
Test flexibly with AspectJ and mock objects:
http://www-106.ibm.com/developerworks/java/library/j-aspectj2/?loc=j

Introduction to AspectJ
http://www-106.ibm.com/developerworks/library/j-aspectj/index.html
> -----Original Message-----
> From: Avinash Chugh [mailto:avi_chugh@hotmail.com]
> Sent: Thursday, November 21, 2002 5:57 AM
> To: cactus-user@jakarta.apache.org
> Subject: Error accessing same http session across test methods
>
>
> Environment: Weblogic 7, Win2k, Cactus 1.4.1
>
> I have a ServletTestCase with two test methods - testLogin(),
> testLogout().
>
> testlogin calls a doPost() on a servlet which logs in user using HTTP
> request parameters, and places an object in session. When I test
> assertNotNull for that session attribute, the test succeeds.
>
> testlogout() calls another doPost() which logs out the earlier user and
> invalidate the session. But when I do assertNotNull() for the same
> session attribute (before the actual call to servlet), the test fails..
>
> I am using the same servlet...and session.isNew() always returns true
>
> This leads me to believe..
> The HTTPSession created by testLogin() can nolonger be accessed by
> subsequent test method(s) ??
>
> This is a bug..right ??
>
> Regards,
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>