莊周夢蝶

          生活、程序、未來
             :: 首頁 ::  ::  :: 聚合  :: 管理
              在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模式)。
             
              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);
              }

              這里實(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模式呢?

          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(
          thisnew Class[0]);
                  }
                  
          catch (InvocationTargetException e) {
                      e.fillInStackTrace();
                      
          throw e.getTargetException();
                  }
                  
          catch (IllegalAccessException e) {
                      e.fillInStackTrace();
                      
          throw e;
                  }
              }

             
          主站蜘蛛池模板: 开鲁县| 米林县| 西乡县| 汝城县| 共和县| 兰考县| 闽清县| 临夏县| 德州市| 永德县| 和顺县| 凤凰县| 航空| 青冈县| 定南县| 奉节县| 康马县| 汉中市| 遵义市| 从化市| 清丰县| 浙江省| 沽源县| 麻江县| 滨州市| 石景山区| 乾安县| 墨江| 会泽县| 沾化县| 大同县| 乃东县| 密山市| 县级市| 日土县| 富锦市| 临高县| 岑巩县| 安平县| 巴林右旗| 蒲江县|