技术开发 频道

实战每晚构建之主要技术

    Ant配置脚本的面向对象性

    从上面可以知道一个ant的配置脚本可以由多个配置文件组成,一个配置文件由目标和属性定义语句组成。我们可以把属性看成是面向对象中的成员变量,目标看成是方法,这样一个配置文件就定义了一个"类",而且它的成员都是静态的,就是说不需要生成"对象"。一个类是可以运行的如果它的配置文件的优异元素是<project>,这就好像我们的java类实现了public static void main(String[] args)方法一样。可以用xml中的定义和引用实体的方式来申明一个"类"继承了另一个"类",这样我们可以实现面向对象当中的"类继承层次图";我们可以用<ant>任务来实现跨对象之间的调用(要求这些对象的类是可以运行的),这样就形成了"对象协作图";我们可以用<antcall>和目标的depends属性来实现对象内部的"方法调用"。

    注意Ant配置脚本的面向对象模型没办法实现方法重载或覆盖。

    1.2 junit单元测试

    大部分集成工具都集成了junit单元测试插件,并有向导帮助写单元测试。Junit发行包的文档很详细地介绍了Junit的设计概念和所使用的设计模式。在这里我简单地说明如何写测试用例、在ant配置文件中调用测试用例和产生测试报告的方法。

    写测试用例

    下面是在eclipse junit向导对MyCode类编写的测试用例TestMyCode文件基础上写的代码:

 

    import junit.framework.TestCase;

    
/*

    * Created on 2003-4-30

    *

    * To change the template for this generated file go to

    * Window>Preferences>Java>Code Generation>Code and Comments

    
*/

    
/**

    *
@author gongys

    *

    * To change the template for this generated type comment go to

    * Window>Preferences>Java>Code Generation>Code and Comments

    
*/

    
public class TestMyCode extends TestCase {

    MyCode myFixture
=null;

    
/**

    * Constructor for TestTest.

    *
@param arg0

    
*/

    
public TestTest(String arg0) {

    
super(arg0);

    }

    
/*

    * @see TestCase#setUp()

    
*/

    
protected void setUp() throws Exception {

    
super.setUp();

    myFixture
= new MyCode();

    System.out.println(
"setup");

    }

    
/*

    * @see TestCase#tearDown()

    
*/

    
protected void tearDown() throws Exception {

    
super.tearDown();

    myFixture
= null;

    System.out.println(
"teardown");

    }

    
public void testSetName() {

    myFixture.setName(
"gongys")

    assertEquals(
"gongys", myFixture.getName());

    System.out.println(
"testSetName");

    System.out.println(
this.getName());

    }

    
public void testSetAge() {

    System.out.println(
"testSetAge");

    myFixture.setAge (
12)

    assertEquals(
12,myFixture.getAge());

    System.out.println(
this.getName());

    }

    }

    有几点需要特殊指出:

    一个TestCase子类中可以包含多个test方法,test方法的原型必须是public void testXXX();

    在执行过程中,junit框架为每一个test方法实例化一个TestCase子类;

    执行testCase的顺序如下:setUp(),testXXX(),teardown();

    fixture是指为每一个测试方法准备的东西:比如数据库连接,此时的目标等,一般在setUp()中设置,testXXX()中使用,teardown()中释放。

    运行这个测试的结果如下:

    setup

    testSetName

    testSetName

    teardown

    setup

    testSetAge

    testSetAge

    teardown

0
相关文章