JUnit源碼分析 (三)——Template Method模式
Posted on 2007-04-06 09:54 dennis 閱讀(2667) 評論(0) 編輯 收藏 所屬分類: 模式與架構(gòu) 、源碼解讀 在JUnit執(zhí)行測試時(shí),我們經(jīng)常需要初始化一些環(huán)境供測試代碼使用,比如數(shù)據(jù)庫連接、mock對象等等,這些初始化代碼應(yīng)當(dāng)在每一個(gè)測試之前執(zhí)行并在測試方法運(yùn)行后清理。在JUnit里面就是相應(yīng)的setUp和tearDown方法。如果沒有這兩個(gè)方法,那么我們要在每個(gè)測試方法的代碼內(nèi)寫上一大堆重復(fù)的初始化和清理代碼,這是多么愚蠢的做法。那么JUnit是怎么讓setUp和tearDown在測試執(zhí)行前后被調(diào)用的呢?
如果你查看下TestCase方法,你會發(fā)現(xiàn)TestCase和TestSuite的run()方法都是將執(zhí)行測試的任務(wù)委托給了TestResult,由TestResult去執(zhí)行測試代碼并收集測試過程中的信息(這里用到了Collecting Parameter模式)。
我們直接找到TestResult,看看它的run方法:
這里實(shí)例化了一個(gè)內(nèi)部類,內(nèi)部類實(shí)現(xiàn)了Protectable接口的 protect()方法,并執(zhí)行傳入的TestCase的runBare()方法,顯然,真正的測試代碼在TestCase的runBare()方法中,讓我們來看下:
Template Method模式
類行為模式的一種
1.意圖:定義一個(gè)操作中的算法的骨架,而將一些延遲步驟到子類中。Template Method使得子類可以不改變一個(gè)算法的結(jié)構(gòu)即可重定義該算法的某些步驟。
2.適用場景:
1)一次性實(shí)現(xiàn)算法的不變部分(基本骨架),將可變的行為留給子類來完成
2)子類中的公共部分(比如JUnit中的初始化和清理)被抽取到一個(gè)公共父類中以避免代碼重復(fù)。
3)控制了子類的擴(kuò)展,這里其實(shí)也有類似回調(diào)函數(shù)的性質(zhì),具體步驟先在骨架中注冊,在具體執(zhí)行時(shí)被回調(diào)。
3.UML圖和結(jié)構(gòu)

抽象父類定義了算法的基本骨架(模板方法),而不同的子類實(shí)現(xiàn)具體的算法步驟,客戶端由此可以與算法的更改隔離。
4.效果:
1)模板方法是代碼復(fù)用的基本技術(shù),在類庫中經(jīng)常使用,可以減少大量的代碼重復(fù)
2)通過隔離算法的不變和可變部分,增加了系統(tǒng)的靈活性,擴(kuò)展算法的某些步驟將變的很容易。
了解了Template Method模式之后,讓我們回到JUnit的源碼,看看runTest()方法,這里主要應(yīng)用的是java的反射技術(shù),對于學(xué)習(xí)反射技術(shù)的有參考價(jià)值:
如果你查看下TestCase方法,你會發(fā)現(xiàn)TestCase和TestSuite的run()方法都是將執(zhí)行測試的任務(wù)委托給了TestResult,由TestResult去執(zhí)行測試代碼并收集測試過程中的信息(這里用到了Collecting Parameter模式)。
public TestResult run() {
TestResult result= createResult();
run(result);
return result;
}
/**
* Runs the test case and collects the results in TestResult.
* This is the template method that defines the control flow
* for running a test case.
*/
public void run(TestResult result) {
result.run(this);
}
TestResult result= createResult();
run(result);
return result;
}
/**
* Runs the test case and collects the results in TestResult.
* This is the template method that defines the control flow
* for running a test case.
*/
public void run(TestResult result) {
result.run(this);
}
我們直接找到TestResult,看看它的run方法:
/**
* Runs a TestCase.
*/
protected void run(final TestCase test) {
startTest(test);
Protectable p = new Protectable() {
public void protect() throws Throwable {
test.runBare();
}
};
runProtected(test, p);
endTest(test);
}
* Runs a TestCase.
*/
protected void run(final TestCase test) {
startTest(test);
Protectable p = new Protectable() {
public void protect() throws Throwable {
test.runBare();
}
};
runProtected(test, p);
endTest(test);
}
這里實(shí)例化了一個(gè)內(nèi)部類,內(nèi)部類實(shí)現(xiàn)了Protectable接口的 protect()方法,并執(zhí)行傳入的TestCase的runBare()方法,顯然,真正的測試代碼在TestCase的runBare()方法中,讓我們來看下:
//將被子類實(shí)現(xiàn)
protected void setUp() throws Throwable {
}
//同上,將被具體的TestCase實(shí)現(xiàn)
protected void tearDown() throws Throwable {
}
/**
* 模板方法
* Runs the bare test sequence.
* @exception Throwable if any exception is thrown
*/
public void runBare() throws Throwable {
setUp();
try {
runTest();
}
finally {
tearDown();
}
}
真相水落石出,對于每一個(gè)測試方法,都遵循這樣的模板:setUp->執(zhí)行測試 runTest()->tearDown。這正是模板方式模式的一個(gè)應(yīng)用例子。什么是template method模式呢?protected void setUp() throws Throwable {
}
//同上,將被具體的TestCase實(shí)現(xiàn)
protected void tearDown() throws Throwable {
}
/**
* 模板方法
* Runs the bare test sequence.
* @exception Throwable if any exception is thrown
*/
public void runBare() throws Throwable {
setUp();
try {
runTest();
}
finally {
tearDown();
}
}
Template Method模式
類行為模式的一種
1.意圖:定義一個(gè)操作中的算法的骨架,而將一些延遲步驟到子類中。Template Method使得子類可以不改變一個(gè)算法的結(jié)構(gòu)即可重定義該算法的某些步驟。
2.適用場景:
1)一次性實(shí)現(xiàn)算法的不變部分(基本骨架),將可變的行為留給子類來完成
2)子類中的公共部分(比如JUnit中的初始化和清理)被抽取到一個(gè)公共父類中以避免代碼重復(fù)。
3)控制了子類的擴(kuò)展,這里其實(shí)也有類似回調(diào)函數(shù)的性質(zhì),具體步驟先在骨架中注冊,在具體執(zhí)行時(shí)被回調(diào)。
3.UML圖和結(jié)構(gòu)

抽象父類定義了算法的基本骨架(模板方法),而不同的子類實(shí)現(xiàn)具體的算法步驟,客戶端由此可以與算法的更改隔離。
4.效果:
1)模板方法是代碼復(fù)用的基本技術(shù),在類庫中經(jīng)常使用,可以減少大量的代碼重復(fù)
2)通過隔離算法的不變和可變部分,增加了系統(tǒng)的靈活性,擴(kuò)展算法的某些步驟將變的很容易。
了解了Template Method模式之后,讓我們回到JUnit的源碼,看看runTest()方法,這里主要應(yīng)用的是java的反射技術(shù),對于學(xué)習(xí)反射技術(shù)的有參考價(jià)值:
protected void runTest() throws Throwable {
Method runMethod= null;
try {
runMethod= getClass().getDeclaredMethod(fName, new Class[0]);
} catch (NoSuchMethodException e) {
fail("Method \""+fName+"\" not found");
}
if (runMethod != null && !Modifier.isPublic(runMethod.getModifiers())) {
fail("Method \""+fName+"\" should be public");
}
try {
runMethod.invoke(this, new Class[0]);
}
catch (InvocationTargetException e) {
e.fillInStackTrace();
throw e.getTargetException();
}
catch (IllegalAccessException e) {
e.fillInStackTrace();
throw e;
}
}
Method runMethod= null;
try {
runMethod= getClass().getDeclaredMethod(fName, new Class[0]);
} catch (NoSuchMethodException e) {
fail("Method \""+fName+"\" not found");
}
if (runMethod != null && !Modifier.isPublic(runMethod.getModifiers())) {
fail("Method \""+fName+"\" should be public");
}
try {
runMethod.invoke(this, new Class[0]);
}
catch (InvocationTargetException e) {
e.fillInStackTrace();
throw e.getTargetException();
}
catch (IllegalAccessException e) {
e.fillInStackTrace();
throw e;
}
}