1. 程式人生 > >JUnit4.8.2原始碼分析-4 RunNotifier與RunListener

JUnit4.8.2原始碼分析-4 RunNotifier與RunListener

JUnit4執行過程中,org.junit.runner.notification. RunListener和RunNotifier運用了觀察者模式

1.觀察者

觀察者Observer/Listener主要作用是分析各種事件並定義相應的回撥介面。例如JDK中MouseListener處理滑鼠鍵相關的5個動作:滑鼠鍵被按下/pressed、釋放/released、單擊/clicked、游標進入或離開某元件/enters or exits。java.awt.event .MouseListener的原始碼:

public interface MouseListener extendsEventListener {

    publicvoid mouseClicked(MouseEvent e);

    publicvoid mousePressed(MouseEvent e);

    publicvoid mouseReleased(MouseEvent e);

    publicvoid mouseEntered(MouseEvent e);

    publicvoid mouseExited(MouseEvent e);

}

那麼,RunListener處理測試執行的7個動作

1. publicvoid testRunStarted(Description description)

在所有測試將要執行前的動作。如同運動會比賽前召開開幕式一樣。

2. public void testStarted(Description description)

在一個測試(如@Test)開始之前的動作。

3. public void testFinished(Description description)

對應testStarted,一個測試結束後的動作。不論測試succeeds or fails。

4.public void testRunFinished(Result result)

對應testRunStarted,所有測試執行後的動作。

5.public void testIgnored(Description description)

遇到一個@Ignore測試方法是的動作。

6. public void testFailure(Failurefailure)

若測試失敗,呼叫這個動作。

7. public void testAssumptionFailure(Failure failure)

與斷言不同,Assume定義了4個靜態的測試條件,如assumeTrue(boolean b)等。如果條件不滿足時呼叫本方法。

RunListener定義了這7個空方法,地位等同於MouseAdapter,yqj2065覺得最好還是用abstract修飾它。

注意:回撥介面的引數,用於將資料傳遞給上層模組。由於7個動作發生的時機不同,RunListener中使用了Description、Failure和Result封裝回調介面所需的資料。

org.junit.runner.notification.Failure封裝的資料有:final Description、final Throwable。

Result封裝的資料有:

       privateAtomicInteger fCount // the number of tests run

       privateAtomicInteger fIgnoreCount// the number of tests ignored

       privatefinal List<Failure> fFailures

       privatelong fRunTime// milliseconds for run the entire suite

       privatelong fStartTime;

Result有一些get方法,還提供了幾個便利方法如

public booleanwasSuccessful() //fFailures .size()為0f返回true

還有一個自帶的私有內部類Listener,用於產生Result封裝的資料。例如

public voidtestFinished(Description description) throws Exception {

              fCount.getAndIncrement();

       }

把這個程式碼放在testStarted中也可以。(可以刪除這些型別)

2. TextListener

具體監聽器org.junit.internal.TextListener將以列印文字的形式處理7種動作。

正如我們常用的System.out.println(),TextListener的列印工作由一個java.io.PrintStream完成,而該物件由System或JUnitSystem指定。順便說明介面JUnitSystem有兩個方法:exit(int i)和PrintStream out();其子類RealSystem程式碼

public class RealSystem implements JUnitSystem {

       publicvoid exit(int code) {   System.exit(code);  }

       publicPrintStream out() {            returnSystem.out;  }

}

TextListener為編寫我們自己的Listener提供了一個簡單的例子。(可以刪除這些型別)

3. RunNotifier

被觀察目標Subject/Notifier,某種事件發生或資料/狀態改變後,自動呼叫doNotify()轉而呼叫回撥。RunNotifier是一個半截子的Subject,它維護一個登錄檔List<RunListener>,有addListener、removeListener操作;但是它的7個fireXxx方法觸發對回撥介面的呼叫,不涉及某種事件發生或資料/狀態改變。這就是典型的二傳手式委派。真正的幕後的Subject是誰呢?

因此這個二傳手是一個孤零零的類,沒有子類,所有public方法都在註釋中聲稱為Internaluse only。

本文涉及的型別:org.junit.runner.notification.RunListener及其子類org.junit.internal.TextListener(JUnitSystem和RealSystem)、資料Description、Failure和Result、RunNotifier

涉及的設計模式:觀察者模式。