隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創作品(標有[原]字樣)轉載時請注明出處,謝謝。

          常用鏈接

          常用設置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 155795
          • 排名 - 390

          最新評論

          [標題]:[原]深入JUnit4.x
          [時間]:2009-7-5
          [摘要]:JUnit4.x參數化測試、私有方法測試、測試套件
          [關鍵字]:JUnit、Test、測試、單元測試、addTest not applicable、suite、套件、參數化測試、私有方法、private、反射、斷言
          [環境]:JUnit4.5、MyEclipse7
          [作者]:Winty (wintys@gmail.com) http://www.aygfsteel.com/wintys

          [正文]:
          測試目標類:
          Calculator.java:
          package wintys.junit;

          /**
           *
           * @author Winty (wintys@gmail.com) http://www.aygfsteel.com/wintys/
           * @version 2009-07-05
           */
          public class Calculator {
              /**
               * 加法
               * @param a
               * @param b
               * @return a與b的和
               */
              public int add(int a , int b){
                  return a + b;
              }
              
              /**
               * 減法,訪問權限設為private
               * @param a
               * @param b
               * @return a與b的差
               */
              private int sub(int a , int b){
                  return a - b;
              }
          }

          簡單測試
          CalculatorTest.java:
          package wintys.junit;

          import junit.framework.Assert;

          import org.junit.Before;
          import org.junit.Test;

          public class CalculatorTest {
              Calculator cal;

              @Before
              public void setUp() throws Exception {
                  cal = new Calculator();
              }
              
              @Test
              public void testAdd(){
                  int result = cal.add(1, 1);
                  Assert.assertEquals(2 , result);
              }
          }

          參數化測試CalculatorTestWithParameter.java:
          package wintys.junit;

          import java.util.Arrays;
          import java.util.Collection;

          import static org.junit.Assert.assertEquals;
          import org.junit.Test;
          import org.junit.runner.RunWith;
          import org.junit.runners.Parameterized;
          import org.junit.runners.Parameterized.Parameters;

          /**
           * JUnit4參數化測試
           * @author Winty (wintys@gmail.com) http://www.aygfsteel.com/wintys/
           * @version 2009-07-05
           */
          @RunWith(Parameterized.class)
          public class CalculatorTestWithParameter {
              private int input1;
              private int input2;
              private int result;
              
              public CalculatorTestWithParameter(int input1, int input2, int result) {
                  super();
                  this.input1 = input1;
                  this.input2 = input2;
                  this.result = result;
              }
              
              @Parameters
              public static Collection<Object[]> initParam(){
                  Object[][] objArray = new Object[][]{
                          {1 , 1 , 2},
                          {2 , 5 , 7},
                          {-1 , 8 , 7},
                          {-5 , -1 ,-6}
                  };
                  
                  return Arrays.asList(objArray);
              }
              
              @Test
              public void testAdd(){
                  Calculator cal = new Calculator();
                  int rt = cal.add(input1, input2);
                  
                  assertEquals(result , rt);
              }
          }


          測試私有方法CalculatorTestOfPrivate.java:
          package wintys.junit;

          import java.lang.reflect.Method;

          import junit.framework.Assert;

          import org.junit.Before;
          import org.junit.Test;

          /**
           * JUnit4測試private方法
           * @author Winty (wintys@gmail.com) http://www.aygfsteel.com/wintys/
           * @version 2009-07-05
           */
          public class CalculatorTestOfPrivate {
              Calculator cal;

              @Before
              public void setUp() throws Exception {
                  cal = new Calculator();
              }
              
              @Test
              public void testPrivateMethod(){
                  Class<Calculator> cls = Calculator.class;
                  Method method = null;
                  Object result = 0 ;
                  try {
                      method = cls.getDeclaredMethod("sub", new Class<?>[]{int.class,int.class});
                      method.setAccessible(true);
                      result = (Object)method.invoke(cal, 1 , 2);
                      
                      Assert.assertEquals(-1, result);
                  } catch (Exception e) {
                      e.printStackTrace();
                      Assert.fail();
                  }     
                  
              }

          }


          建立測試套件AllTests.java:
          package wintys.junit;

          import org.junit.runner.RunWith;
          import org.junit.runners.Suite;
          import org.junit.runners.Suite.SuiteClasses;

          /**
           * JUnit4 Test Suite Style
           * @author Winty (wintys@gmail.com) http://www.aygfsteel.com/wintys/
           * @version 2009-07-04
           */
          //表示這個類是一個測試套件
          @RunWith(Suite.class)
          //說明這個測試套件所包含的測試類
          @SuiteClasses({
              SubscriptionTest.class,
              CalculatorTest.class,
              CalculatorTestWithParameter.class,
              CalculatorTestOfPrivate.class
          })
          public class AllTests {
              //在JUnit4.x中,套件類的內容一般留空,只作為以上Annotation的持有者
              // the class remains completely empty,
              // being used only as a holder for the above annotations
          }



          附錄:
          JUnit3.8.x測試套件語法:
          package test;

          import test.gg.ba.util.UtilityTest;
          import junit.framework.Test;
          import junit.framework.TestSuite;

          public class MyTests {
              public static Test suite() {
                  TestSuite suite = new TestSuite("Test for test");
                  //$JUnit-BEGIN$

                  suite.addTest(test.gg.ba.util.UtilityTest.class);

                  //$JUnit-END$
                  return suite;
              }
          }


          在JUnit4中運行JUnit3.8.x的測試套件,
          Runner for use with JUnit 3.8.x-style:
          @RunWith(AllTests.class)
           public class ProductTests {
              public static junit.framework.Test suite() {
                 ...
              }
           }


          [參考資料]:
          [1] Test suites using annotations  : http://radio.javaranch.com/lasse/2006/07/27/1154024535662.html
          [2] JUnit 4 Test Suite - addTest not applicable : http://www.velocityreviews.com/forums/t636846-junit-4-test-suite-addtest-not-applicable.html

          [附件]:
          源程序:http://www.aygfsteel.com/Files/wintys/junit_test.zip

          原創作品,轉載請注明出處。
          作者:Winty (wintys@gmail.com)
          博客:http://www.aygfsteel.com/wintys

          posted on 2009-07-08 10:28 天堂露珠 閱讀(1605) 評論(0)  編輯  收藏 所屬分類: Test

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 潢川县| 沁水县| 彭州市| 华安县| 岗巴县| 东乌珠穆沁旗| 罗源县| 周口市| 长兴县| 渝中区| 松潘县| 蒙阴县| 七台河市| 江孜县| 新泰市| 元江| 潢川县| 湟源县| 临海市| 梁平县| 二连浩特市| 资阳市| 大竹县| 山西省| 平邑县| 南宁市| 武强县| 福清市| 淮安市| 隆回县| 台江县| 格尔木市| 长武县| 固安县| 松原市| 红桥区| 盐津县| 汝州市| 平远县| 黄骅市| 利川市|