The session object is a simple Map, so creating it is easy.

First, you have to define an instance variable in which to store the session, and second, you need to implement the setSession() method.

public class MyAction implements SessionAware {
   private Map session;

   public void setSession(Map session) {
       this.session = session;
    }

}

If most/all of your actions need access to the session, you may want to define a BaseAction that implements SessionAware, and add a protected method, getSession(). Or, you could make the session variable protected (instead of private), but I prefer getSession().

In your unit test:

    Map session = new HashMap();
    session.put("key", value);
    action.setSession(session);

    String result = action.execute();
    assertEquals(SUCCESS, result);

What the map contains are the keys and values your action expects. These are the same things you'd pass to HttpSession.setAttribute (String name, Object value). So, say you want to store a shopping cart object in the session, you'd do:

    session.put("shoppingCart", shoppingCart);
    action.setSession(session);



On Sep 13, 2007, at 7:04 PM, Session A Mwamufiya wrote:

Thanks for the tip, but I'll need an actual code snippet, because when I make my action implement SessionAware, I need to also implement the setSession() method, and I'm back to square 1, trying to figure out how to create a session object. I can send a HashMap to the method, but what does it have to contain? What is the actual setting that needs to go in it, since I need to implement it?


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]





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to