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.

http://struts.apache.org/2.0.6/struts2-core/apidocs/org/apache/ 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 difficult."

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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to