`

Ant应用

阅读更多
官方网站手册:http://ant.apache.org/manual/index.html

一、ant中使用junit 
两种方法来执行测试:

一是使用源路径

  <batchtest fork="yes" todir="${reports.tests}">    <fileset dir="${src.tests}">      <include name="**/*Test*.java"/>      <exclude name="**/AllTests.java"/>    </fileset>  </batchtest></junit> 
二是使用编译路径

    <batchtest>       <fileset dir="${build.dir}">           <include name="**/*Test.*" />           <exclude name="**/Jdbc*Test.*" />       </fileset>    </batchtest> 
Ant自学教程的部分代码:

    <property name="report.dir"  value="${build.dir}/junitreport"/>    ...    <target name="junit" depends="jar">        <mkdir dir="${report.dir}"/>        <junit printsummary="yes">            <classpath>                <path refid="classpath"/>                <path refid="application"/>            </classpath>                        <formatter type="xml"/>                        <batchtest fork="yes" todir="${report.dir}">                <fileset dir="${src.dir}" includes="*Test.java"/>            </batchtest>        </junit>    </target>        <target name="junitreport">        <junitreport todir="${report.dir}">            <fileset dir="${report.dir}" includes="TEST-*.xml"/>            <report todir="${report.dir}"/>        </junitreport>    </target> 
示例一

    <target name="tests" depends="build, buildtests" description="Run tests">       <junit printsummary="on" fork="false" haltonfailure="false" failureproperty="tests.failed" showoutput="true">           <!--classpath refid="master-classpath" / -->           <classpath refid="test-classpath" />           <formatter type="brief" usefile="false" />           <batchtest>              <fileset dir="${build.dir}">                  <include name="**/*Tests.*" />                  <exclude name="**/Jdbc*Tests.*" />              </fileset>           </batchtest>       </junit>       <fail if="tests.failed">            tests.failed=${tests.failed}            ****  One or more tests failed!  Check the output ...  ****        </fail>
    </target>

  
示例二

       <target name="test-unit" depends="build-unit" description="--> Run all unit tests">

              <echo message="Test output can be found in directory ${dist.unitreport.dir}"/>

              <delete dir="${dist.unitreport.dir}"/>

              <mkdir dir="${dist.unitreport.dir}"/>

 
              <echo message="executing tests [${test.match}.java]"/>

 
              <junit printsummary="${junit.printsunmmary}" showoutput="${junit.showoutput}"

                     fork="${junit.fork}" forkmode="${junit.forkmode}" failureproperty="${junit.failureproperty}">

                     <formatter type="plain" usefile="false"/> <!-- just to console -->

                     <formatter type="xml" usefile="true"/>

 
                     <classpath refid="compile.classpath.unit"/>

 
                     <batchtest todir="${dist.unitreport.dir}">

                            <fileset dir="${build.unit.dir}">

                                   <include name="${test.match}.class"/>

                            </fileset>

                     </batchtest>

              </junit>

              <fail if="test.failure">

                 One or more unit tests failed.

           </fail>

       </target>

 
       <target name="report-test-unit" depends="test-unit" description="--> generate unit test report">

              <junitreport todir="${dist.unitreport.dir}">

                     <fileset dir="${dist.unitreport.dir}">

                            <include name="TEST-*.xml"/>

                     </fileset>

                     <report format="frames" todir="${dist.unitreport.dir}"/>

              </junitreport>

              <delete dir="${dist.unitreport.dir}" includes="TEST-*.xml"/>

       </target>

   

二、build.xml文件的编写

1、加载配置文件

        <property file="build.properties"/>2、定义全局变量

3、定义公用classpath信息,供编译时调用4、帮助信息5、建立ant具体的target
       build、deploy、deploywar、clean、undeploy、

6、建立junit测试target(有时包含测试报告)

       buildtests、tests、dbTests

7、建立数据库方面的target

       createTables、dropTables、loadData、printData、clearData、shutdownDb

8、Tomcat tasks

       先配置其classpath;再配置taskdef;最后配置target

       install、reload、start、stop、list

  
三、hsqldb在测试中的应用

1、hsqldb.jar:放置路径springapp/war/WEB-INF/lib/

2、server.bat:springapp/db/

java -classpath ..\war\WEB-INF\lib\hsqldb.jar org.hsqldb.Server -database test
3、编写sql脚本。如

springapp/db/create_products.sql

springapp/db/load_data.sql

4、编写ant脚本

包含属性文件的连接配置:

db.driver=org.hsqldb.jdbcDriverdb.url=jdbc:hsqldb:hsql://localhostdb.user=sadb.pw=
5、启动数据库

open a command window, change to the 'springapp/db' directory and start the database by running one of these startup scripts

6、执行ant脚本

 
如:<target name="dbTests" depends="build, buildtests,dropTables,createTables,loadData" description="Run db tests">

  
四、基于Spring框架的数据库操作测试

1、用到jar包:spring-test.jar;

2、测试类继承AbstractTransactionalDataSourceSpringContextTests;

如:

public class JdbcProductDaoTests extends AbstractTransactionalDataSourceSpringContextTests

 
3、复写父类方法,加载配置及其初始化资源

    private ProductDao productDao;    public void setProductDao(ProductDao productDao) {       this.productDao = productDao;    }    @Override    protected String[] getConfigLocations() {       return new String[] { "classpath:test-context.xml" };    }    @Override    protected void onSetUpInTransaction() throws Exception {       super.deleteFromTables(new String[] { "products" });       super.executeSqlScript("file:db/load_data.sql", true);
    }

3、建立测试方法

 
4、建立配置文件和数据库执行脚本文件

 
5、在build文件中添加相应建立ant脚本

     <path id="test-classpath">       <fileset dir="${web.dir}/WEB-INF/lib">           <include name="*.jar" />        </fileset>       <pathelement path="${build.dir}" />       <pathelement path="${test.dir}" />       <pathelement path="${web.dir}/WEB-INF/classes" />
    </path>

     <target name="dbTests" depends="build, buildtests,dropTables,createTables,loadData" description="Run db tests">       <junit printsummary="on" fork="false" haltonfailure="false" failureproperty="tests.failed" showoutput="true">           <classpath refid="test-classpath" />           <formatter type="brief" usefile="false" />            <batchtest>              <fileset dir="${build.dir}">                  <include name="**/Jdbc*Tests.*" />              </fileset>           </batchtest>        </junit>        <fail if="tests.failed">  One or more tests failed!  Check the output ...  </fail>    </target>
分享到:
评论

相关推荐

    ant应用小例子(适合初学者)

    自己做的ant编译java应用程序小示例!

    ant应用开发

    ant应用 java 自动集成 ant应用 ant应用 ant应用

    Ant应用开发指南(学习ant的必备资料)

    学习ant的好资料。系统讲解了ant的功能体系。

    JAVA_Ant详细介绍与应用

    在本文中,主要让读者熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作。 一.安装与配置 二.Ant的关键元素 三.Ant的常用任务 四.利用Ant构建和部署Java工程 1.利用Ant的javac任务来编译java程序 2.使用...

    ant应用指南

    ant应用指南,适合入门者学习

    Java的Build工具—Ant应用指南(1)

    博文链接:https://chenchuxin.iteye.com/blog/141355

    讲解Ant的应用

    ant的应用,主要讲解ant和项目之间的构建,详细讲解ant在项目开发中的使用

    Ant命令的简介应用之快速入门

    Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建和部署等功能。在本文中,主要让读者熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作。

    ant1.9资源

    在本文中,主要让读者熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作。 一. 安装与配置 下载地址:http://ant.apache.org/,在本文中下载的是1.7.0版本。解压到某个目录(例如E:"apache-ant-1.7.0),...

    Ant基本应用教程学习

    目录 ANT简介 安装Apache Ant 验证Apache Ant的安装 安装 Eclipse Ant构建文件 Ant属性任务 Ant属性文件 Ant数据类型 ...Ant打包应用 Ant部署应用程序 Ant执行Java代码 Ant和Eclipse集成 Ant Junit集成

    Ant脚本详解说明

    Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建和部署等功能。在本文中,主要熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作。

    ant简明教程

    ant应用到java项目中,ant的关键元素讲解

    ant工具可以构建web应用

    ant工具可以构建web应用,ant工具可以构建web应用ant工具可以构建web应用ant工具可以构建web应用ant工具可以构建web应用ant工具可以构建web应用ant工具可以构建web应用ant工具可以构建web应用ant工具可以构建web应用

    Ant安装、部署及应用

    Ant安装、部署及应用详解,本人亲自测试,简单方便构建。

    Ant批量打包Android应用

    Ant批量打包Android 应用教程地址http://blog.csdn.net/zhaokaiqiang1992/article/details/38086747

Global site tag (gtag.js) - Google Analytics