注册本站  论坛  繁體中文

电脑技巧
手机 | MP3 | MP4 | 显卡 | 主板 | 显示器 | 光存储 | 笔记本 | 网络设备 | 移动存储 | 数码相机
键鼠 | CPU | 音箱 | GPS | 电视 | 服务器 | 投影机 | 机箱电源 | 品牌电脑 | 办公打印 |
| 网站首页 | Cisco | Windows | Linux | Java | Dotnet | Oracle | 网页设计 | 平面设计 | 安全 | 软件应用 | 电脑维修 | 办公维修 |
您现在的位置: 电脑技巧 >> Java >> 开源技术 >> ANT >> Java正文

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

文章来源:ChinaITLab 收集整理 作者:佚名 更新时间:2006-1-19 【 】 【加入收藏
一、下载并解压缩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>
  • 上一篇Java:

  • 下一篇Java:
  • 最 新 热 门
     如何在MyEclipse快速搭建Hibernate应用
     spring aop中单独代理和自动代理的设置
     Nhibernate与代码生成器介绍
     Java多线程同步设计中使用Metux
     讨论5种跟踪Java执行的方法
     在Eclipse中使用SWT进行界面设计
     Spring 结合 Hibernate 配置 C3P0
     开发不再是苦差事 用Eclipse简化开发
     解决运行eclipse内存不足的问题
     利用Ecipse生成Javadoc乱码解决方法
    最 新 推 荐
     Java应用利器组合:Ant+JUnit+Cobertura
     使用ant编译打包应用程序
     ant的一些偏门技巧
     Antlr入门详细教程
     ant+cactus+tomcat5.5容器内单元测试
     使用Ant进行增量快速构建实现
     Ant+JMeter进行Web应用的稳定性测试
     关于build tool的构想--从ant说起
     Weblogic Server ant开发Web Service二(图)
     Weblogic Server ant开发Web Service一(图)
    相 关 文 章

    Javascript+DOM访问XML文件数据实例
    javascript+xml实现二级下拉菜单二
    javascript+xml实现二级下拉菜单一
    java多线程设计模式:wait/notify机制
    利用 Ant 和 JUnit 进行增量开发
    Spring中bean的基本xml配置
    Antenna与j2me打包详细介绍
    Eclipse Ant开发EJB的三个习惯
    Enterprise Architecture 简介
    Hibernate 3 Annotations 进阶

    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告

     

    Copyright 2006-2008 pcjx.com All Rights Reserved
    电脑技巧 版权所有 粤ICP备06059145号 地图
    本网站所有内容未经许可不得转载或做其他使用