jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vincent Massol" <vmas...@octo.com>
Subject RE: servlet context listener possible?
Date Sat, 22 Feb 2003 12:51:36 GMT
Hi Gary,

An interesting question! :-)

I'm not sure what your question is so let's assume you want to test
servlet code and this code depends on some Servlet Context state that
you set up using a ContextListener. The other option is that you want to
specifically unit test the ContextListener itself. Let's not explore
this option for the moment (unless you tell me this is what you want to
achieve!).

There are 2 solutions that I can see:

Solution 1: You simply define your ContextListener in your web.xml as
you would would normally do for your runtime application. When the
Cactus Redirectors will be called, your ContextListener will be
triggered. When your method under test is called, your ContextListener
will have run.

Solution 2 (the best solution): Cactus is about unit testing. This means
that you will want to test a given someMethod() method. As with any
JUnit unit test, the first part of your testXXX() should setup any
domain object that is required by the someMethod() method. The
ServletContext is such a domain object. Thus, you simply define it's
state in your testXXX() method before calling someMethod() (Note: You
can of course factorize this in a JUnit setUp() method).

Example:

public void testXXX()
{
   config.getServletContext().setAttribute("", "");
[...]
   someMethod();

   // Perform asserts here
[...]
}

Hope it helps,
-Vincent
 
> -----Original Message-----
> From: Gary Horton [mailto:Gary.Horton@central.sun.com]
> Sent: 06 February 2003 02:24
> To: cactus-user@jakarta.apache.org
> Subject: servlet context listener possible?
> 
> My webapp has a ServletContextListener configured in the web.xml,
which
> is needed to initialize some startup stuff before any test cases can
be
> run correctly. Is it possible with Cactus to tell the container about
> this listener? From my quick overview and browse of examples, this
> answer is not yet obvious to me, so I thank you in advance for any
> insights.
> 
> Thanks,
> Gary Horton
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org



Mime
View raw message