<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 117  文章 - 72  trackbacks - 0

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

    常用鏈接

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

    訂閱

    訂閱

    留言簿(7)

    隨筆分類(130)

    隨筆檔案(123)

    搜索

    •  

    積分與排名

    • 積分 - 155602
    • 排名 - 391

    最新評論

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

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

    /**
     *
     * @author Winty (wintys@gmail.com) http://m.tkk7.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://m.tkk7.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://m.tkk7.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://m.tkk7.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://m.tkk7.com/Files/wintys/junit_test.zip

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

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

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


    網站導航:
     
    主站蜘蛛池模板: 本免费AV无码专区一区| 亚洲色偷偷色噜噜狠狠99网| 色哟哟国产精品免费观看| 午夜网站免费版在线观看| 亚洲av极品无码专区在线观看| 久久不见久久见免费视频7| 4480yy私人影院亚洲| 95免费观看体验区视频| 亚洲黄色在线观看| 91精品国产免费久久久久久青草| 久久久亚洲欧洲日产国码是AV| 香蕉免费一区二区三区| 亚洲人成电影青青在线播放| 野花高清在线观看免费完整版中文| 亚洲日本va在线观看| 在线免费不卡视频| 国产亚洲精品免费| 久久久久亚洲AV综合波多野结衣| 美女网站在线观看视频免费的| 亚洲第一极品精品无码久久| 无码囯产精品一区二区免费 | 久久国产精品免费一区二区三区| 亚洲一区无码精品色| 一级毛片在线免费看| 亚洲熟妇AV一区二区三区浪潮| 又粗又大又猛又爽免费视频| 在线免费观看h片| 亚洲理论片在线观看| 国产真实伦在线视频免费观看| sss日本免费完整版在线观看| 亚洲AV日韩AV鸥美在线观看| 中文字幕无码不卡免费视频| 日韩欧美亚洲中文乱码| 国产av天堂亚洲国产av天堂| 国产免费毛不卡片| 永久免费无码日韩视频| 久久久久亚洲AV片无码下载蜜桃 | 亚洲А∨精品天堂在线| 久久久久久夜精品精品免费啦| 中国亚洲呦女专区| 久久精品国产亚洲精品|