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 Matt Raible <ma...@raibledesigns.com> on 2003/09/18 04:22:15 UTC

Testing Listeners

What is the recommended way to test Listeners with Cactus.  I have a
StartupListener ...

public class StartupListener implements ServletContextListener

... That talks to a database and reads a number of properties files -
and stuffs objects into the servlet context.  I basically just need to
verify that the variables get set correctly.

Thanks,

Matt



RE: Testing Listeners

Posted by Matt Raible <ma...@raibledesigns.com>.
That worked - thanks.

Matt

-----Original Message-----
From: Vincent Massol [mailto:vmassol@pivolis.com] 
Sent: Thursday, September 18, 2003 12:12 AM
To: 'Cactus Users List'
Subject: RE: Testing Listeners


I've never used ServletContextListener but I've had a quick look and it
looks easy to unit test. Let's take an example:

public class MyListener implements ServletContextListener
{
    public void contextDestroyed(ServletContextEvent sce)
    {
      [...]
    }
    public void contextInitialized(ServletContextEvent sce)
    {
      [...]
    }
}

Here's the test:

public class MyListenerTest extends ServletTestCase
{
    public void testXXX()
    {
        // setup
        ServletContextEvent sce = 
            new ServletContextEvent(config.getServletContext());
        MyListener listener = new MyListener();
        
        // test
        listener.contextInitialized(sce);

        // asserts
   }
}

Now, what I don't know is whether you need the ServletContext in a given
state or not?

Is that the kind of test you want?

-Vincent


> -----Original Message-----
> From: Matt Raible [mailto:matt@raibledesigns.com]
> Sent: 18 September 2003 04:22
> To: 'Cactus Users List'
> Subject: Testing Listeners
> 
> What is the recommended way to test Listeners with Cactus.  I have a 
> StartupListener ...
> 
> public class StartupListener implements ServletContextListener
> 
> ... That talks to a database and reads a number of properties files - 
> and stuffs objects into the servlet context.  I basically just need to

> verify that the variables get set correctly.
> 
> Thanks,
> 
> Matt
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-user-help@jakarta.apache.org



RE: Testing Listeners

Posted by Vincent Massol <vm...@pivolis.com>.
I've never used ServletContextListener but I've had a quick look and it
looks easy to unit test. Let's take an example:

public class MyListener implements ServletContextListener
{
    public void contextDestroyed(ServletContextEvent sce)
    {
      [...]
    }
    public void contextInitialized(ServletContextEvent sce)
    {
      [...]
    }
}

Here's the test:

public class MyListenerTest extends ServletTestCase
{
    public void testXXX()
    {
        // setup
        ServletContextEvent sce = 
            new ServletContextEvent(config.getServletContext());
        MyListener listener = new MyListener();
        
        // test
        listener.contextInitialized(sce);

        // asserts
   }
}

Now, what I don't know is whether you need the ServletContext in a given
state or not?

Is that the kind of test you want?

-Vincent


> -----Original Message-----
> From: Matt Raible [mailto:matt@raibledesigns.com]
> Sent: 18 September 2003 04:22
> To: 'Cactus Users List'
> Subject: Testing Listeners
> 
> What is the recommended way to test Listeners with Cactus.  I have a
> StartupListener ...
> 
> public class StartupListener implements ServletContextListener
> 
> ... That talks to a database and reads a number of properties files -
> and stuffs objects into the servlet context.  I basically just need to
> verify that the variables get set correctly.
> 
> Thanks,
> 
> Matt
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org