天天看點

JUnit源碼分析 (三)——Template Method模式

在junit執行測試時,我們經常需要初始化一些環境供測試代碼使用,比如資料庫連接配接、mock對象等等,這些初始化代碼應當在每一個測試之前執行并在測試方法運作後清理。在junit裡面就是相應的setup和teardown方法。如果沒有這兩個方法,那麼我們要在每個測試方法的代碼内寫上一大堆重複的初始化和清理代碼,這是多麼愚蠢的做法。那麼junit是怎麼讓setup和teardown在測試執行前後被調用的呢?

    如果你檢視下testcase方法,你會發現testcase和testsuite的run()方法都是将執行測試的任務委托給了testresult,由testresult去執行測試代碼并收集測試過程中的資訊(這裡用到了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,看看它的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);

    這裡執行個體化了一個内部類,内部類實作了protectable接口的 protect()方法,并執行傳入的testcase的runbare()方法,顯然,真正的測試代碼在testcase的runbare()方法中,讓我們來看下:

        //将被子類實作

    protected void setup() throws throwable {

    //同上,将被具體的testcase實作

    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();

真相水落石出,對于每一個測試方法,都遵循這樣的模闆:setup->執行測試 runtest()->teardown。這正是模闆方式模式的一個應用例子。什麼是template method模式呢?

template method模式

類行為模式的一種

1.意圖:定義一個操作中的算法的骨架,而将一些延遲步驟到子類中。template method使得子類可以不改變一個算法的結構即可重定義該算法的某些步驟。

2.适用場景:

1)一次性實作算法的不變部分(基本骨架),将可變的行為留給子類來完成

2)子類中的公共部分(比如junit中的初始化和清理)被抽取到一個公共父類中以避免代碼重複。

3)控制了子類的擴充,這裡其實也有類似回調函數的性質,具體步驟先在骨架中注冊,在具體執行時被回調。

3.uml圖和結構

JUnit源碼分析 (三)——Template Method模式

  抽象父類定義了算法的基本骨架(模闆方法),而不同的子類實作具體的算法步驟,用戶端由此可以與算法的更改隔離。

4.效果:

1)模闆方法是代碼複用的基本技術,在類庫中經常使用,可以減少大量的代碼重複

2)通過隔離算法的不變和可變部分,增加了系統的靈活性,擴充算法的某些步驟将變的很容易。

    了解了template method模式之後,讓我們回到junit的源碼,看看runtest()方法,這裡主要應用的是java的反射技術,對于學習反射技術的有參考價值:

protected void runtest() throws throwable {

        method runmethod= null;

            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");

            runmethod.invoke(this, new class[0]);

        catch (invocationtargetexception e) {

            e.fillinstacktrace();

            throw e.gettargetexception();

        catch (illegalaccessexception e) {

            throw e;

文章轉自莊周夢蝶  ,原文釋出時間5.17