struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Cushing <>
Subject Re: creating a dummy session object for unit testing
Date Thu, 13 Sep 2007 20:43:33 GMT
If your Action implements SessionAware, you don't need to make a mock  
HttpSession at all. You just make a new HashMap, stuff it with the  
values you want, and call action.setSession(). Just one more of the  
things I love about Struts 2. :)

But there are libraries around that do allow mock HttpSessions.  
Spring was mentioned; MockObjects and Mockrunner each have it, and a  
quick google search for "MockHttpSession" reveals plenty of others.  
Still, I'd recommend the SessionAware. 
struts2/interceptor/SessionAware.html "Note that using this interface  
makes the Action tied to a servlet environment, so it should be  
avoided if possible since things like unit testing will become more  

In my experience, however, that's not the case at all. Using  
SessionAware, from a unit testing perspective, makes your Action less  
dependent on the servlet API, and makes unit testing easier.

On Sep 13, 2007, at 3:47 PM, Session A Mwamufiya wrote:

> Hi,
> How do I go about creating a dummy HttpSession object for unit  
> testing a struts 2 app?
> I have created a dummy HttpServletRequest object, but I can't  
> instanciate an HttpSession in teh Request.getSession() method,  
> because HttpSession is an abstract interface and I can't create an  
> instance of it.
> Any ideas?
> Thanks
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message