Here is how you can test struts action with MockStrutsTest
Create your struts configuration
<struts-config>
<action-mappings>
<action path="/test" type="my.demo.MyAction" >
<forward name="viewCapablity" redirect="false" path="/WEB-INF/jsp/view.jsp"></forward>
<forward name="viewCapablity1" redirect="false" path="/WEB-INF/capability/jsp/viewCapability1.jsp"></forward>
</action>
<action path="/benny" type="com.cisco.cssd.struts.demo.MyAction" >
<forward name="viewCapablity" redirect="false" path="/WEB-INF/jsp/view.jsp"></forward>
<forward name="viewCapablity1" redirect="false" path="/WEB-INF/capability/jsp/viewCapability1.jsp"></forward>
</action>
</action-mappings>
</struts-config>
Develop your struts action classes
package my.struts.demo;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyAction extends Action {
public ActionForward execute(ActionMapping aMapping, ActionForm aForm,
HttpServletRequest aRequest, HttpServletResponse aResponse) {
String type = (String) aRequest.getParameter("type");
System.out.println(type);
return aMapping.findForward("viewCapablity");
}
}
write mock tests
package com.cisco.cssd.struts.demo;
import org.junit.Test;
import servletunit.struts.MockStrutsTestCase;
public class TestMyAction extends MockStrutsTestCase {
public TestMyAction(String testName) {
super(testName);
// TODO Auto-generated constructor stub
}
public void setUp() throws Exception {
super.setUp();
setServletConfigFile("/WEB-INF/web.xml");
setConfigFile("my/struts/demo/struts-config.xml");
setInitParameter("validating", "false");
setRequestPathInfo("/test.do");
}
@Test
public void testing () {
System.out.println("testing ...");
addRequestParameter("type", "MY demo type");
actionPerform ();
verifyForward("viewCapablity");
verifyForwardPath("/WEB-INF/jsp/view.jsp");
}
}
finally integrate with the final product.
No comments:
Post a Comment