单元测试工具Junit源代码学习

Package framework
    Class&Interface Hierachy
 
Class Hierarchy
.class java.lang.Object
     .class junit.framework.Assert
            .class junit.framework.TestCase(implements junit.framework.Test)
     .class junit.framework.TestFailure
     .class junit.framework.TestResult
     .class junit.framework.TestSuite(implements junit.framework.Test)
     .class java.lang.Throwable(implements java.io.Serializable)
            .class java.lang.Error
                   .class junit.framework.AssertionFailedError
 
Interface Hierarchy
     .interface junit.framework.Protectable
     .interface junit.framework.Test
     .interface TestListener
 
Interface Test
主要方法
countTestCases:统计TestCases数目
run:运行测试并将结果返回到指定的TestResult中
 
Class Assert
首先,Assert提供的public方法都可以带或不带自己定义的提示;其次,Assert中的Assert方法是protected的,这意味着Assert是一个静态类,它提供的方法都是Static的.
 
public方法
assert:保留(deprecated)方法,判断一个条件是否为真.
assertTrue:assert的替代方法,判断一个条件是否为真.
assertEquals:用于判断实际值和期望值是否相同(Equals),可以是各种JAVA对象.
assertNotNull:判断一个对象是否不为空.
assertNull:判断一个对象是否为空.
assertSame:判断实际值和期望值是否为同一个对象(==),注意与assertEquals区分.
fail:直接返回失败,抛出AssertionFailedError.
 
private方法
failNotEquals:主要用于assertEquals方法,调用fail返回失败提示.
failNotSame:主要用于assertSame方法,调用fail返回失败提示.
 
Class AssertionFailedError
AssertionFailedError是从Jdk提供Error类简单继承而来,主要方法如下:
    public AssertionFailedError(String message){
    super(message);
    }
    Class Assert中比较失败都是抛出AssertionFailedError.
 
Interface Protectable
这个接口使用了一种比较少见的用法.
在Interface本身只定义了一个方法,
    public abstract void protect() throws Throwable;注意方法throws的是所有Error和Exception的祖先。通过这个定义可以保证运行的时候,如果出现任何Error和Exception,都将被抛出而不会导致程序不能继续运行。
    Protectable的接口没有被framework包中的任何类实现,他的使用在类TestResult中的run方法中。以下是run方法中的代码:
    protected void run(final TestCase test) {
    startTest(test);
    Protectable p=new Protectable() {
        public void protect() throws Throwable {
        test.runBare();
        }
    }
    runProtected(test, p);
    endTest(test);
    }
这里实际是声明了一个Anonymous Class,实现了Interface Protectable.
 
Interface TestListener
Interface TestListener的用途和他名称一样,主要用于运行时刻监听,BaseRunner(所有运行类,如TestRunner)实现了这一接口。由于运行是通过TestResult来实现,只要调用TestResult.addListener就可以增加监听,TestResut会调用接口中相应的方法,具体见TestResut。主要方法:
-public-
addError:增加错误,注意这里的错误应该指测试程序本身的错误或被测试程序错误,而不是测试失败.
endTest:结束测试
startTest:开始测试
 
Class TestCase
使用者最主要使用的类,继承Class Assert,实现Interface Test。主要方法有:
-public-
TestCase:创建本身,可以指定TestCase准备运行的测试方法名称,保存在私有属性fName.
countTestCases:返回TestCase数目,直接返回1.
name:deprecated,建议使用getName,返回当前准备允许的测试方法名称(私有属性fName).
run:运行TestCase,如果没有指定结果存储的TestResult,将调用CreateResult方法。注意,TestCase和TestResult互相调用,整个运行流程如下:
    1、TestCase.run调用TestResult.run
    2、TestResult.run调用TestResult.StartTest
    3、TestResult.run创建一个Anonymous类,实现接口Protectable
    4、在Protectable.protect方法中,调用TestCase.runBare
    5、通过运行Protectable.runBare调用runBare,通过Exception捕获增加错误及失败报告
其中,
    runBare:不使用TestResult直接运行
    runTest:运行测试,注意每调用runTest只运行当前fName指定的方法
    etName:返回fName
    setName:设置fName
-protected-
createResult:创建一个TestResult
setUp:在运行runTest前调用
tearDown:在运行runTest后调用
 
Class TestFailure
用于存放测试对比失败信息的类。主要为Class TestResult调用。主要属性有,
protected Test fFailedTest;
protected Throwable fThrownException;
fFailedTest存放失败的TestCase信息,fThrownException存放失败提示信息,主要方法有:
-public-
TestFailure:初始化,对fFailedTest、fThrownException赋值.
failedTest:返回fFailedTest.
thrownException:返回fThrownException.
toString:
 
Class TestResult
TestResult用于运行并收集测试结果(通过Exception捕获),注意interface.
TestListener的所有方法在这里都有同名方法,并在同名方法中被调用,主要属性有:
-protectd Vector fFailures:测试失败报告保存
-protectd Vector fErrors:测试错误报告保存
-protectd Vector fListeners:测试监听器保存
-protectd int fRunTests:运行的测试
-private boolean fStop:是否应该停止测试的标志
主要方法:
-public-
TestResult:初始化
addError:synchronized方法,增加一个错误并向所有监听程序发送错误,调用TestListener.addError.
addFailure:synchronized方法,增加一个失败并向所有监听程序发送错误,调用TestListener.addFailure.
addListener:synchronized方法,增加监听程序.
removeListener:synchronized方法,移走监听程序.
endTest:结束测试,并通知所有监听程序,调用TestListener.endTest.
errorCount:synchronized方法,返回错误个数.
errors:synchronized方法,用Enumeration返回所有错误.
failureCount:synchronized方法,返回失败个数.
failures:synchronized方法,用Enumeration返回所有失败.
run:运行测试,创建一个Anonymous类,实现接口Protectable,然后调用.
runProtected方法,可以参考TestCase的run方法.
runCount:synchronized方法,返回运行数量.
runProtected:实际运行测试.
runTests:deprecated方法,被runCount方法替代.
shouldStop:synchronized方法,返回是否应该停止测试.
startTest:开始测试,并通知所有监听程序,调用TestListener.startTest.
stop:synchronized方法,设置停止标志fStop为真。注意,是否停止测试TestResult不负责的,stop只是简单设置停止标志。
testErrors:deprecated synchronized方法,被errorCount替代.
failureCount:deprecated synchronized方法,被testFailures替代.
wasSuccessful:synchronized方法,如果所有运行过的测试方法都通过,返回真,否则返回否.
-private-
cloneListeners:复制fListeners,主要用于要使用监听列表的endTest、startTest、addError、addFailure.
 
Class TestSuite
TestSuite用于将多个TestCase集合起来放在一起管理,TestSuite在增加TestCase的时候,实际已经将TestCase实例化(实例化次数依据包括方法的数量).主要属性有:
-private Vector fTests=new Vector(10):存放TestCase的实例
-private String fName:TestSuite名称.
主要方法有:
-public-
TestSuite:初始化,可以选择空、指定名称或包括指定的类。如果是指定的类,那么在TestSuite初始化的时候,TestCase已经实例化,并加入到fTests中。
addTest:增加一个TestCase/TestSuite的实例到fTests中。注意,由于TestCase的实例化实际上只指定一个测试方法,即增加一个TestCase的实例是注册了其中一个测试方法,参考TestCase类。若参数是一个TestSuite,则相当于增加了一个子Suite.
addTestSuite:增加一个子Suite,实际效果同参数为TestSuite的addTest.
countTestCases:返回Suite(包括子Suite)中的TestCase实例(测试方法)数量.
run:运行测试,注意,这里是运行fTests中的所有测试,用了TestResult.
shouldStop:方法来判断是否终止运行,实际是调用了runTest.
runTest:运行某一TestCase或子Suite的测试,注意使用了递归。如果参数test是一个TestSuite,会再调用TestSuite.run.
testAt:返回fTests指定顺序的TestCase或TestSuite.
testCount:返回fTests大小,注意和countTestCases的区别.
tests:返回fTests的内容.
setName:设置名称.
getName:增加名称.
toString:
-private-
addTestMethod:增加一个测试方法(TestCase实例)到fTests.
exceptionToString:返回一个Throwable中的提示信息.
getConstructor:返回指定类的构造函数.
isPublicTestMethod:判断一个方法是否是public的测试方法,即一个函数是否是public的,同时也是一个测试方法.
isTestMethod:判断一个方法是否是测试方法,即以"test"为前缀、没有参数及返回值.
warning:增加一个错误提示TestCase到fTest中,注意,这里也使用了Anonymous Class.warning使用主要在对TestSuite操作的时候,不会因为有错就终止操作,而是在run的时候报告错误.
 
continued........
next page from'Package extensions'
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值