本文共 1994 字,大约阅读时间需要 6 分钟。
public class UserAction { public String list(){ return "success"; } } |
package unittest; import org.apache.struts2.StrutsTestCase; import org.junit.Test; import action.UserAction; import com.opensymphony.xwork2.ActionProxy; public class ActionUnitTest extends StrutsTestCase { // 重写父类方法,指定配置文件的名字 protected String[] getContextLocations() { return new String[] { "struts.xml" }; } @Test public void testExecute() throws Exception { ActionProxy proxy = getActionProxy("/unitTest"); UserAction = (UserAction) proxy.getAction(); assertNotNull(test); String result = proxy.execute(); assertEquals("success", result); } } |
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!– Development Mode –> <constant name="struts.devMode" value="true" /> <package name="manager" namespace="/" extends="struts-default">//不是default <action name="unitTest" class="action.UserAction" method="list"> <result name="success">unitTest.jsp </result> </action> </package> </struts> |
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> 访问URL:http://localhost:8080/Struts2UnitDemo/unitTest.action |
最新内容请见作者的GitHub页:http://qaseven.github.io/
转载地址:http://bdhax.baihongyu.com/