Apache Struts 2 Documentation > Home > FAQs > How do I unit test my action's validation logic |

Apache Struts 2 Documentation
How do I unit test my action's validation logic
Edit Page Browse Space Add Page Add News
Added by tm_jee, last edited by Ted Husted on Jul 22, 2006 (view change)

    public class WebLoginActionTest extends TestCase {

        private WebLoginAction wla;

        protected void setUp() throws Exception {
            wla = new WebLoginAction();
            wla.setJ_username("");
            wla.setJ_password(null);
            super.setUp();
        }

        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());
        }
    }

Contributor: Carlos