Announcement Announcement Module
Collapse
No announcement yet.
Testing Actions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment

    Working...
    X