Results 1 to 2 of 2

Thread: Testing Actions

  1. #1
    Join Date
    May 2005
    Location
    Fallbrook, CA
    Posts
    22

    Default Testing Actions

    OK, I searched the Spring forums for an answer before I posted this so maybe its never been asked before.

    I'm trying to use JUnit to test an action which extends AbstractAction using code like the following:
    Code:
    public class TestStandardVariableAction extends TestCase {
    
    	public void testDoExecuteRequestContext() throws Exception {
    		MockRequestContext context = new MockRequestContext();
    		
    		StandardVariableAction action = new StandardVariableAction();
    		Event event = action.execute(context);
    .
    .
    .
    I'm assuming I need to tell event to return me some information in order to determine whether the action did what it is supposed to do. The methods that look like they ought to be what I need (getParameter, getAttribute, getParameters) all return null when I pass the name of known parameters. What am I doing wrong?

    BTW, here's a snippet of the code that inserts data into the RequestContext from the action's doExecute method.
    Code:
    now.put("ampm",cal.get(Calendar.AM_PM));
    context.getRequestScope().setAttribute("now",now);

  2. #2
    Join Date
    Sep 2004
    Location
    Leuven, Belgium
    Posts
    1,853

    Default

    Basically you're on the right track:

    1) setup a request context with some data that the action will act on
    2) configure your action as necessary (e.g. set collaborators)
    3) execute the action with the request context
    4) verify that execution was as expected

    The tricky part is ofcourse step 4. For complex cases you might need things like mock objects. For a simple case where you're just putting some data in the request scope, you can ofcouse do things like:

    Code:
    assertNotNull(context.getRequestScope().getAttribute("now");
    Note that I'm pulling data from the context, not the event! This is normal since you're action is putting stuff in the context, not the event. If you would have an action that returns an event with certain parameters, you can ofcouse also test those.

    Erwin

Similar Threads

  1. Multiple actions
    By bytecount in forum Web Flow
    Replies: 1
    Last Post: Jul 27th, 2005, 10:34 AM
  2. Testing Struts Actions
    By bryanross in forum Container
    Replies: 4
    Last Post: Mar 13th, 2005, 07:54 PM
  3. Please help! Unit testing code for JPetStore
    By lakershen in forum Container
    Replies: 4
    Last Post: Jan 13th, 2005, 05:00 PM
  4. Replies: 10
    Last Post: Nov 2nd, 2004, 09:38 AM
  5. Replies: 4
    Last Post: Oct 20th, 2004, 05:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •