qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          單元測試Struts2的Action(包含源碼)

           很久沒有從頭搭建Struts2的環境了。最近,認真實踐了單元測試Struts2、Spring等Java項目。
            今天特意寫的是單元測試Struts2的Action,遇到了不少問題,果然是實踐出真知啊。
            從搭建環境、寫代碼到寫這篇文章,一共花了90分鐘。
            特別說明:本文是原創,搭建環境、寫代碼、運行,都是實踐并且正確的。
            本文是靠譜的,而非簡單的復制-粘貼。
            1.新建工程,加入相關jar包。
            struts.core等struts自己的jar包
            spring-core-3.2.0.RELEASE.jar
            (用到了里面的一些類,比如看起來挺奇怪的,測試Struts2怎么和Spring扯上關系了。
            沒有這個包,會報錯java.lang.NoClassDefFoundError: org/springframework/core/io/ResourceLoader)
            spring-test-3.2.3.RELEASE.jar
            Junit的jar包
            Tomcat的Server Rumtime lib。
            2.新建Action。
          public class UserAction {
          public String list(){
          return "success";
          }
          }
            3.新建單元測試
          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 test = (UserAction) proxy.getAction();
          assertNotNull(test);
          String result = proxy.execute();
          assertEquals("success", result);
          }
          }


           4.Struts配置。
          <?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>
            5.訪問web頁面測試。
            需要在Web.xml中增加
          <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

          posted on 2013-09-30 11:32 順其自然EVO 閱讀(377) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 翁源县| 阿克苏市| 易门县| 平阳县| 抚顺县| 仁化县| 舟曲县| 全椒县| 留坝县| 观塘区| 宜阳县| 江达县| 灵山县| 醴陵市| 万盛区| 沙坪坝区| 富平县| 大埔县| 新余市| 行唐县| 六枝特区| 鹤峰县| 万全县| 定边县| 兰西县| 桐梓县| 安达市| 巴塘县| 新疆| 德保县| 辛集市| 广丰县| 共和县| 张家口市| 沭阳县| 茌平县| 三穗县| 翁牛特旗| 邳州市| 七台河市| 公主岭市|