qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

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

           很久沒有從頭搭建Struts2的環(huán)境了。最近,認真實踐了單元測試Struts2、Spring等Java項目。
            今天特意寫的是單元測試Struts2的Action,遇到了不少問題,果然是實踐出真知啊。
            從搭建環(huán)境、寫代碼到寫這篇文章,一共花了90分鐘。
            特別說明:本文是原創(chuàng),搭建環(huán)境、寫代碼、運行,都是實踐并且正確的。
            本文是靠譜的,而非簡單的復制-粘貼。
            1.新建工程,加入相關(guān)jar包。
            struts.core等struts自己的jar包
            spring-core-3.2.0.RELEASE.jar
            (用到了里面的一些類,比如看起來挺奇怪的,測試Struts2怎么和Spring扯上關(guān)系了。
            沒有這個包,會報錯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 閱讀(376) 評論(0)  編輯  收藏


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


          網(wǎng)站導航:
           
          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 拉萨市| 南投市| 高州市| 纳雍县| 九寨沟县| 股票| 左贡县| 溆浦县| 文安县| 田阳县| 安平县| 温州市| 肇庆市| 大关县| 玉门市| 邯郸市| 靖江市| 无为县| 日土县| 玉龙| 景泰县| 中西区| 五寨县| 赞皇县| 美姑县| 陆河县| 霍林郭勒市| 开封县| 手游| 吴堡县| 象山县| 子长县| 云霄县| 独山县| 临西县| 淮滨县| 松潘县| 沽源县| 怀远县| 宣恩县| 江北区|