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

Posted on 2009-06-22 15:06 林光炎 阅读(426) 评论(0)  编辑  收藏 所属分类: JAVA
 一、下载并解压缩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-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>
  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, 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是服务器的端口号,${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>
  文章来源: baike.duba.net

posts - 104, comments - 33, trackbacks - 0, articles - 0

Copyright © 林光炎