首页 / 软件开发 / 数据结构与算法 / ant+cactus+tomcat5.5容器内单元测试
ant+cactus+tomcat5.5容器内单元测试2011-04-01一、下载并解压缩cactus下载地址为http://java.chinaitlab.com/tools/45970.html。将cactus的lib目录下的cactus-ant-1.7.1.jar复制到ant的lib目录。二、配置cactuscactus的配置很简单,新建一个cactus.properties文件,并把它放在ant脚本中的cactus任务的classpath下,文件中包括如下内容cactus.sysproperties=cactus.contextURL
#cactus-sample-servlet-cactified就是你的测试应用所在路径,8080是端口号
cactus.contextURL = http://localhost:8080/cactus-sample-servlet-cactified
cactus.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>