Introduction
Struts 2 supports running unit tests of methods in the Struts Action class with the Struts 2 JUnit plugin. The JUnit plugin allows you to test methods of an Action class from within the Struts 2 framework. The Struts Servlet filter and interceptors fire just as if your application was running on a Servlet container.
...
Code Block |
---|
|
<action name="register" class="org.apache.struts.register.action.Register" method="execute">
<result name="success">/thankyou.jsp</result>
<result name="input">/register.jsp</result>
</action>
|
...
Code Block |
---|
| java |
---|
| java |
---|
title | testExecuteValidationPasses method from RegisterTest class |
---|
|
@Test
public void testExecuteValidationPasses() throws Exception() {
request.setParameter("personBean.firstName", "Bruce");
request.setParameter("personBean.lastName", "Phillips");
request.setParameter("personBean.email", "bphillips@ku.edu");
request.setParameter("personBean.age", "19");
ActionProxy actionProxy = getActionProxy("/register.action");
Register action = (Register) actionProxy.getAction() ;
assertNotNull("The action is null but should not be.", action);
String result - actionProxy.execute();
assertEquals("The execute method did not return " + ActionSupport.SUCCESS + " but should have.", ActionSupport.SUCCESS, result);
}
|
...
Code Block |
---|
| java |
---|
| java |
---|
title | testExecuteValidationFailsMissingFirstName method from RegisterTest class |
---|
|
@Test
public void testExecuteValidationFailsMissingFirstName() throws Exception() {
//request.setParameter("personBean.firstName", "Bruce");
request.setParameter("personBean.lastName", "Phillips");
request.setParameter("personBean.email", "bphillips@ku.edu");
request.setParameter("personBean.age", "19");
ActionProxy actionProxy = getActionProxy("/register.action");
Register action = (Register) actionProxy.getAction() ;
assertNotNull("The action is null but should not be.", action);
String result - actionProxy.execute();
assertEquals("The execute method did not return " + ActionSupport.INPUT + " but should have.", ActionSupport.INPUT, result);
}
|
...