关闭

ant+cactus+tomcat5.5容器内单元测试

发表于:2007-10-31 12:27

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

一、下载并解压缩cactus
    下载地址为http://java.chinaitlab.com/tools/45970.html 。将cactus的lib目录下的cactus-ant-1.7.1.jar复制到ant的lib目录。

二、配置cactus
    cactus的配置很简单,新建一个cactus.properties文件,并把它放在ant脚本中的cactus任务的classpath下,文件中包括如下内容

cactus.sysproperties=cactus.contextURL #cactus-sample-servlet-cactified就是你的测试应用所在路径,8080是端口号 cactus.contextURL=http://localhost:8080/cactus-sample-servlet-cactifiedcactus.servletRedirectorName=ServletRedirector cactus.jspRedirectorName=JspRedirector cactus.filterRedirectorName=FilterRedirector
    具体的做法结合ant脚本再进一步解释。

三、运行ant脚本

  ant脚本主要执行以下任务

1、设定classpath
<path id="project.classpath"><fileset dir="${lib.dir}"><include name="*.jar"/></fileset><!--cactus.properties文件就需要放在lib.dir所对应的路径中--><pathelement location="${lib.dir}"/><pathelement location="${tomcat.home}/common/lib/jsp-api.jar"/><pathelement location="${tomcat.home}/common/lib/servlet-api.jar"/></path>
2、定义相关任务
<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、编译应用的类文件和测试的类文件

4、打包整个应用为war文件

    需要注意的是,不仅要打包应用类,测试类也要打包
<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}"/><!--别忘了打包测试类--><classes dir="${target.classes.test.dir}"/><!--别忘了打包各种相关的jar文件--><lib dir="project.classpath"/></war></target>

5、在应用的web.xml文件中添加测试所需的各种映射

    cactus提供了两个task来完成这个工作,CactifyWar和WebXmlMerge。
    CactifyWar的功能是自动在已经打包的应用的web.xml文件中添加所需的映射。WebXmlMerge是提供合并两个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、运行测试
    cactus提供了cactus和RunServerTests两个task来运行测试。

    "cactus" task是通过复制容器服务器的最小文件并运行来运行测试,因此需要制定容器服务器的类型,启动速度稍快点,另外配置比较方便,但是无法测试象tomcat连接池等资源。另外对tomcat5.5的支持也不好。

    "RunServerTests"是通过直接启动容器服务起来运行测试,因此速度稍慢,且配置较麻烦,但能测试各种资源。
<target name="test"depends="test.prepare"description="Run tests on Tomcat"><!--Start the servlet engine, waitforit to be started, run the unit tests, stop the servlet engine, waitforit to be stopped. The servlet engineisstoppedifthe tests failforany reason--><!--8080是服务器的端口号,${project.name}-cactified是项目的路径,和上一步的cactifywar 的destfile相对应--><runservertests testURL="http://localhost:8080/${project.name}-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="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>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号