ant+cactus+tomcat5.5容器內(nèi)單元測(cè)試簡(jiǎn)明手冊(cè)

          摘要:

          折騰了一個(gè)星期,終于搞定ant+cactus+tomcat5.5容器內(nèi)單元測(cè)試,為感謝cleverpig斑竹的熱心回貼,特首發(fā)于matrix apache版。
          折騰了一個(gè)星期,終于搞定ant+cactus+tomcat5.5容器內(nèi)單元測(cè)試,為感謝cleverpig斑竹(http://blog.matrix.org.cn/page/cleverpig)的熱心回貼,特首發(fā)于matrix apache版。關(guān)于ant的使用,請(qǐng)搜索ant的使用手冊(cè),網(wǎng)上大把中文的。

          一、下載并解壓縮cactus
          下載地址為http://apache.freelamp.com/jakarta/cactus/binaries/jakarta-cactus-12-1.7.1.zip。將cactus的lib目錄下的cactus-ant-1.7.1.jar復(fù)制到ant的lib目錄。


          二、配置cactus
          cactus的配置很簡(jiǎn)單,新建一個(gè)cactus.properties文件,并把它放在ant腳本中的cactus任務(wù)的classpath下,文件中包括如下內(nèi)容
          cactus.sysproperties=cactus.contextURL
          #cactus-sample-servlet-cactified就是你的測(cè)試應(yīng)用所在路徑,8080是端口號(hào)
          cactus.contextURL = http://localhost:8080/cactus-sample-servlet-cactified
          cactus.servletRedirectorName = ServletRedirector
          cactus.jspRedirectorName = JspRedirector
          cactus.filterRedirectorName = FilterRedirector

          具體的做法結(jié)合ant腳本再進(jìn)一步解釋。

          三、運(yùn)行ant腳本
          ??ant腳本主要執(zhí)行以下任務(wù)

          1、設(shè)定classpath
          ????
          <path id="project.classpath">
          ????????<fileset dir="${lib.dir}">
          ?????????? <include name="*.jar"/>
          ????????</fileset>
          ????????<!-- cactus.properties文件就需要放在lib.dir所對(duì)應(yīng)的路徑中 -->
          ????????<pathelement location="${lib.dir}"/>
          ????????<pathelement location="${tomcat.home}/common/lib/jsp-api.jar"/>
          ????????<pathelement location="${tomcat.home}/common/lib/servlet-api.jar"/>
          ????</path>


          2、定義相關(guān)任務(wù)
          ??
          <taskdef resource="cactus.tasks" classpathref="project.classpath"/>
          ?? <taskdef name="runservertests" classname="org.apache.cactus.integration.ant.RunServerTestsTask">
          ????????????<classpath>
          ????????????????<path refid="project.classpath"/>
          ????????????</classpath>
          ????????</taskdef>


          3、編譯應(yīng)用的類文件和測(cè)試的類文件

          4、打包整個(gè)應(yīng)用為war文件
          需要注意的是,不僅要打包應(yīng)用類,測(cè)試類也要打包
          ??
          ??
          <target name="war" depends="compile.java"
          ????????????description="Generate the runtime war">

          ????????<war warfile="${target.dir}/${project.name}.war"
          ???????????? webxml="${src.webapp.dir}/WEB-INF/web.xml">
          ????????????<fileset dir="${src.webapp.dir}">
          ????????????????<exclude name="cactus-report.xsl"/>
          ????????????????<exclude name="WEB-INF/cactus-web.xml"/>
          ????????????????<exclude name="WEB-INF/web.xml"/>
          ????????????</fileset>
          ????????????<classes dir="${target.classes.java.dir}"/>
          ????????????<!-- 別忘了打包測(cè)試類 -->
          ????????????<classes dir="${target.classes.test.dir}"/>
          ????????????<!-- 別忘了打包各種相關(guān)的jar文件 -->
          ????????????< lib dir="project.classpath"/>
          ????????</war>
          ????</target>


          5、在應(yīng)用的web.xml文件中添加測(cè)試所需的各種映射
          cactus提供了兩個(gè)task來完成這個(gè)工作,CactifyWar和WebXmlMerge。
          CactifyWar的功能是自動(dòng)在已經(jīng)打包的應(yīng)用的web.xml文件中添加所需的映射。WebXmlMerge是提供合并兩個(gè)web.xml文件的功能。
          <target name="test.prepare"
          ????????????depends="war, compile.cactus, test.prepare.logging">

          ????????<!-- Cactify the web-app archive -->
          ????????<cactifywar srcfile="${target.dir}/${project.name}.war"
          ????????????????????destfile="${tomcat.home}/webapps/${project.name}-cactified.war"
          ????????????????>
          ????????????<classes dir="${target.classes.java.dir}"/>
          ????????????<classes dir="${target.classes.test.dir}"/>
          ????????????<lib dir="project.classpath"/>
          ?????? </cactifywar>
          </target>


          6、運(yùn)行測(cè)試
          cactus提供了cactus和RunServerTests兩個(gè)task來運(yùn)行測(cè)試。
          "cactus" task是通過復(fù)制容器服務(wù)器的最小文件并運(yùn)行來運(yùn)行測(cè)試,因此需要制定容器服務(wù)器的類型,啟動(dòng)速度稍快點(diǎn),另外配置比較方便,但是無法測(cè)試象tomcat連接池等資源。另外對(duì)tomcat5.5的支持也不好。
          "RunServerTests"是通過直接啟動(dòng)容器服務(wù)起來運(yùn)行測(cè)試,因此速度稍慢,且配置較麻煩,但能測(cè)試各種資源。
          ??
          <target name="test" depends="test.prepare"
          ???????????? description="Run tests on Tomcat ">

          ????????<!-- Start the servlet engine, wait for it to be started, run the
          ???????????? unit tests, stop the servlet engine, wait for it to be stopped.
          ???????????? The servlet engine is stopped if the tests fail for any reason -->
          ????????<!-- 8080是服務(wù)器的端口號(hào),${project.name}-cactified是項(xiàng)目的路徑,和上一步的cactifywar 的destfile相對(duì)應(yīng) -->
          ????????<runservertests
          ????????????????testURL="http://localhost:8080/${project.name}-cactified/ServletRedirector?Cactus_Service=RUN_TEST"
          ????????????????startTarget="_StartTomcat"
          ????????????????stopTarget="_StopTomcat"
          ????????????????testTarget="_Test"/>

          ????</target>

          ????
          ????
          <!-- _Test就是一個(gè)普通的junit任務(wù) -->
          ????<target name="_Test">
          ????????<junit printsummary="yes" fork="yes">
          ????????????<classpath>
          ????????????????<path refid="project.classpath"/>
          ????????????????<pathelement location="${target.classes.java.dir}"/>
          ????????????????<pathelement location="${target.classes.test.dir}"/>
          ????????????</classpath>
          ????????????<formatter type="brief" usefile="false"/>
          ????????????<formatter type="xml"/>

          ????????????<batchtest>
          ????????????????<fileset dir="${src.test.dir}">
          ????????????????????<!-- Due to some Cactus synchronization bug, the 'unit' tests need
          ??????????????to run before the 'sample' tests -->
          ????????????????????<include name="**/Test*.java"/>
          ????????????????????<exclude name="**/Test*All.java"/>
          ????????????????</fileset>
          ????????????</batchtest>
          ????????</junit>
          ????</target>


          posted on 2006-06-12 11:48 nbt 閱讀(231) 評(píng)論(0)  編輯  收藏 所屬分類: Java2EE

          <2006年6月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          Java技術(shù)網(wǎng)站

          友情鏈接

          國(guó)內(nèi)一些開源網(wǎng)站

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 宁化县| 吴忠市| 专栏| 宁都县| 松江区| 军事| 沾益县| 广东省| 托里县| 襄垣县| 楚雄市| 蛟河市| 凤山县| 铁岭县| 云梦县| 普宁市| 合山市| 锦屏县| 合阳县| 澎湖县| 涿州市| 兴城市| 曲水县| 深圳市| 沈丘县| 吉林市| 财经| 濮阳市| 彰化市| 崇义县| 新兴县| 皋兰县| 甘南县| 左贡县| 西平县| 珠海市| 定安县| 天峻县| 河津市| 莱阳市| 仙桃市|