熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

ant+cactus+tomcat5.5容器內單元測試

2022-06-13   來源: Java開源技術 
下載並解壓縮cactus
下載地址為 將cactus的lib目錄下的cactusantjar復制到ant的lib目錄


配置cactus
cactus的配置很簡單新建一個cactusproperties文件並把它放在ant腳本中的cactus任務的classpath下文件中包括如下內容

cactussysproperties=ntextURL
#cactussampleservletcactified就是你的測試應用所在路徑是端口號
ntextURL =//localhost:/cactussampleservletcactified
cactusservletRedirectorName = ServletRedirector
cactusjspRedirectorName = JspRedirector
cactusfilterRedirectorName = FilterRedirector

具體的做法結合ant腳本再進一步解釋

運行ant腳本
  ant腳本主要執行以下任務

設定classpath
<path id=projectclasspath>
        <fileset dir=${libdir}>
           <include name=*jar/>
        </fileset>
        <! cactusproperties文件就需要放在libdir所對應的路徑中 >
        <pathelement location=${libdir}/>
        <pathelement location=${tomcathome}/common/lib/jspapijar/>
        <pathelement location=${tomcathome}/common/lib/servletapijar/>
    </path>
定義相關任務
<taskdef resource=cactustasks classpat/>
   <taskdef name=runservertests classname=orgapachecactusintegrationantRunServerTestsTask>
            <classpath>
                <path refid=projectclasspath/>
            </classpath>
        </taskdef>
編譯應用的類文件和測試的類文件

打包整個應用為war文件
需要注意的是不僅要打包應用類測試類也要打包
<target name=war depends=compilejava
            description=Generate the runtime war>

        <war warfile=${targetdir}/${projectname}war
             webxml=${srcwebappdir}/WEBINF/webxml>
            <fileset dir=${srcwebappdir}>
                <exclude name=cactusreportxsl/>
                <exclude name=WEBINF/cactuswebxml/>
                <exclude name=WEBINF/webxml/>
            </fileset>
            <classes dir=${targetclassesjavadir}/>
            <! 別忘了打包測試類 >
            <classes dir=${targetclassestestdir}/>
            <! 別忘了打包各種相關的jar文件 >
            < lib dir=projectclasspath/>
        </war>
    </target>

在應用的webxml文件中添加測試所需的各種映射
cactus提供了兩個task來完成這個工作CactifyWar和WebXmlMerge
CactifyWar的功能是自動在已經打包的應用的webxml文件中添加所需的映射WebXmlMerge是提供合並兩個webxml文件的功能
<target name=testprepare
            depends=war compilecactus testpreparelogging>

        <! Cactify the webapp archive >
        <cactifywar srcfile=${targetdir}/${projectname}war
                    destfile=${tomcathome}/webapps/${projectname}cactifiedwar
                >
            <classes dir=${targetclassesjavadir}/>
            <classes dir=${targetclassestestdir}/>
            <lib dir=projectclasspath/>
       </cactifywar>
</target>

運行測試
cactus提供了cactus和RunServerTests兩個task來運行測試
cactus task是通過復制容器服務器的最小文件並運行來運行測試因此需要制定容器服務器的類型啟動速度稍快點另外配置比較方便但是無法測試象tomcat連接池等資源另外對tomcat的支持也不好
RunServerTests是通過直接啟動容器服務起來運行測試因此速度稍慢且配置較麻煩但能測試各種資源
<target name=test depends=testprepare
             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 >
        <! 是服務器的端口號${projectname}cactified是項目的路徑和上一步的cactifywar 的destfile相對應 >
        <runservertests
                testURL=//localhost:/${projectname}cactified/ServletRedirector?Cactus_Service=RUN_TEST
                startTarget=_StartTomcat
                stopTarget=_StopTomcat
                testTarget=_Test/>

    </target>

<! _Test就是一個普通的junit任務 >
    <target name=_Test>
        <junit printsummary=yes fork=yes>
            <classpath>
                <path refid=projectclasspath/>
                <pathelement location=${targetclassesjavadir}/>
                <pathelement location=${targetclassestestdir}/>
            </classpath>
            <formatter type=brief usefile=false/>
            <formatter type=xml/>

            <batchtest>
                <fileset dir=${srctestdir}>
                    <! Due to some Cactus synchronization bug the unit tests need
              to run before the sample tests >
                    <include name=**/Test*java/>
                    <exclude name=**/Test*Alljava/>
                </fileset>
            </batchtest>
        </junit>
    </target>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28518.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.