Dashboard > WebWork > ... > FAQ > How do I unit test my action's validation logic
  WebWork Log In View a printable version of the current page.  
  How do I unit test my action's validation logic
Added by tm_jee, last edited by Jasper Rosenberg on Oct 01, 2006  (view change)
Labels: 
(None)

public class WebLoginActionTest extends TestCase {

    private WebLoginAction wla; //Your custom WW2 action that extends ActionSupport
    // If the WW2 action contains other complex objects (i.e. domain model objects), setUp() can be used to initialize those objects
    protected void setUp() throws Exception {
        wla = new WebLoginAction();
        wla.setJ_username("");
        wla.setJ_password(null);
        super.setUp();
    }

    // The WiKi shows different code for testing the validation for an action, the one below is proven to work
    public void testWebLoginActionValidation() throws ValidationException {
        ActionValidatorManager avm = ActionValidatorManagerFactory.getInstance();
        avm.validate(wla,"");
        Map fieldErrors = wla.getFieldErrors();

        assertTrue(wla.hasErrors());
        assertEquals(2, fieldErrors.size());
        assertTrue(fieldErrors.containsKey("j_username"));
        assertTrue(fieldErrors.containsKey("j_password"));

        System.out.println("[errors] : " +  fieldErrors.toString()); // Displays validated fields and its associated error messages
    }
}

 

 

Contributor: Carlos Cajina

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators