aldream

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            1 Posts :: 10 Stories :: 1 Comments :: 0 Trackbacks
          使用JUnit時,您主要都是透過繼承TestCase類別來撰寫測試案例,預(yù)設(shè)上您可以使用testXXX() 名稱來撰寫單元測試。

          在測試一個單元方法時,有時您會需要給它一些物件作為運(yùn)行時的資料,例如您撰寫下面這個測試案例:

          MaxMinTest.java
           1 package onlyfun.caterpillar.test;
           2 
           3 import onlyfun.caterpillar.MaxMinTool;
           4 import junit.framework.TestCase;
           5  
           6 public class MaxMinTest extends TestCase {
           7     public void testMax() {
           8         int[] arr = {-5-4-3-2-1012345};
           9         assertEquals(5, MaxMinTool.getMax(arr));
          10     }
          11 
          12     public void testMin() {
          13         int[] arr = {-5-4-3-2-1012345};
          14         assertEquals(-5, MaxMinTool.getMin(arr));
          15     }
          16     
          17     public static void main(String[] args) {
          18         junit.swingui.TestRunner.run(MaxMinTest.class);
          19     }
          20 }

          您將設(shè)計的MaxMinTool包括靜態(tài)方法getMax()與getMin(),當(dāng)您給它一個整數(shù)陣列,它們將個別傳回陣列中的最大值與最小值,顯然 的,您所準(zhǔn)備的陣列重複出現(xiàn)在兩個單元測試之中,重複的程式碼在設(shè)計中可以減少就儘量減少,在這兩個單元測試中,整數(shù)陣列的準(zhǔn)備是單元方法所需要的資源, 我們稱之為fixture,也就是一個測試時所需要的資源集合。

          fixture必須與上下文(Context)無關(guān),也就是與程式執(zhí)行前後無關(guān),這樣才符合單元測試的意涵,為此,通常將所需的fixture撰寫在單元方法之中,如此在單元測試開始時創(chuàng)建fixture,並於結(jié)束後銷毀fixture。

          然而對於重複出現(xiàn)在各個單元測試中的fixture,您可以集中加以管理,您可以在繼承TestCase之後,重新定義setUp()tearDown()方法,將數(shù)個單元測試所需要的fixture在setUp()中創(chuàng)建,並在tearDown()中銷毀,例如:

          MaxMinTest.java
           1 package onlyfun.caterpillar.test;
           2 
           3 import onlyfun.caterpillar.MaxMinTool;
           4 import junit.framework.TestCase;
           5 
           6 public class MaxMinTest extends TestCase {
           7     private int[] arr;
           8 
           9     protected void setUp() throws Exception {
          10         super.setUp();
          11         arr = new int[]{-5-4-3-2-1012345};
          12     }
          13 
          14     protected void tearDown() throws Exception {
          15         super.tearDown();
          16         arr = null;
          17     }
          18     
          19     public void testMax() {
          20         assertEquals(5, MaxMinTool.getMax(arr));
          21     }
          22 
          23     public void testMin() {
          24         assertEquals(-5, MaxMinTool.getMin(arr));
          25     }
          26     
          27     public static void main(String[] args) {
          28         junit.swingui.TestRunner.run(MaxMinTest.class);
          29     }
          30 }

          setUp()方法會在每一個單元測試testXXX()方法開始前被呼叫,因而整數(shù)陣列會被建立,而tearDown()會在每一個單元測試 testXXX()方法結(jié)束後被呼叫,因而整數(shù)陣列參考名稱將會參考至null,如此一來,您可以將fixture的管理集中在 setUp()與tearDown()方法之後。

          最後按照測試案例的內(nèi)容,您完成MaxMinTool類別:

          MaxMinTool.java
           1 package onlyfun.caterpillar;
           2 
           3 public class MaxMinTool {
           4     public static int getMax(int[] arr) {
           5         int max = Integer.MIN_VALUE;
           6         
           7         for(int i = 0; i < arr.length; i++) {
           8             if(arr[i] > max)
           9                 max = arr[i];
          10         }
          11         
          12         return max;
          13     }
          14     
          15     public static int getMin(int[] arr) {
          16         int min = Integer.MAX_VALUE;
          17         
          18         for(int i = 0; i < arr.length; i++) {
          19             if(arr[i] < min)
          20                 min = arr[i];
          21         }
          22         
          23         return min;
          24     }
          25 }

          Swing介面的TestRunner在測試失敗時會顯示紅色的棒子,而在測試成功後會顯示綠色的棒子,而 "Keep the bar green to keep the code clean." 正是JUnit的名言,也是測試的最終目的。
          posted on 2007-11-11 00:55 aldream 閱讀(597) 評論(0)  編輯  收藏 所屬分類: javaJUnit測試
          主站蜘蛛池模板: 龙江县| 三门县| 翼城县| 德化县| 金坛市| 丹巴县| 扬州市| 蒙城县| 甘肃省| 衡水市| 岱山县| 龙里县| 秦安县| 乌拉特后旗| 周宁县| 永泰县| 沅江市| 阿巴嘎旗| 光泽县| 鞍山市| 静海县| 乳源| 达日县| 东源县| 通江县| 金溪县| 子长县| 海南省| 佛山市| 临沂市| 乾安县| 礼泉县| 即墨市| 柞水县| 孙吴县| 东阿县| 巫山县| 瓮安县| 忻州市| 漯河市| 泸定县|