想飛就別怕摔

          大爺?shù)牟M罵人

          Junit簡(jiǎn)單例子

          本例是加減乘除計(jì)算
          先看一個(gè)簡(jiǎn)單的類Calculator.java:
           1package com.test.junit;
           2/**
           3 * 
           4 * 數(shù)學(xué)計(jì)算
           5 *
           6 */

           7public class Calculator {
           8    public int add(int a,int b){
           9        return a + b;
          10    }

          11    public int minus(int a,int b){
          12        return a - b;
          13    }

          14    public int multiply(int a, int b ){
          15        return a * b;
          16    }

          17    public int divide(int a , int b )throws Exception
          18    {
          19        if(0 == b){
          20            throw new Exception("除數(shù)不能為零");
          21        }

          22        return a / b;
          23    }

          24/*    
          25 * 一下是不用IDE給我們提供的Junit,用Junit本身去測(cè)試的3個(gè)方法。
          26    public static void main (String[]args){
          27        junit.textui.TestRunner.run(CalculatorTest.class);
          28        junit.swingui.TestRunner.run(CalculatorTest.class);
          29        junit.awtui.TestRunner.run(CalculatorTest.class);
          30    }
          31*/

          32}

          下面我們看看傳統(tǒng)的JUnit的TestCase:
          CalculatorTest.java:
           1package com.test.junit;
           2
           3import junit.framework.Assert;
           4import junit.framework.TestCase;
           5/**
           6 * Keep the bar green to keep the code clean
           7 * 
           8 *在Junit3.8中必須繼承TestCase類
           9 *
          10 *單元測(cè)試不是證明您是對(duì)的,而是證明您沒有錯(cuò)
          11 */

          12public class CalculatorTest extends TestCase {
          13    /**
          14     * 在Junit3.8中,測(cè)試方法滿足如下原則
          15     * 1)public
          16     * 2)void 
          17     * 3)無方法參數(shù)
          18     * 4)最重要的方法名稱必須以test開頭
          19     */

          20    private Calculator cal;
          21    
          22    //在執(zhí)行每個(gè)test之前,都執(zhí)行setUp;
          23    public void setUp(){
          24         cal = new Calculator();
          25    }

          26    
          27    //在執(zhí)行每個(gè)test之后,都要執(zhí)行tearDown
          28    public void tearDown(){
          29    }

          30
          31    public void testAdd()
          32    {
          33//        Calculator cal = new Calculator();
          34        int result = cal.add(12);
          35        //斷言assert
          36        Assert.assertEquals(3, result);
          37    }
           
          38    
          39    public void testMinus()
          40    {
          41//        Calculator cal = new Calculator();
          42        int result = cal.minus(52);
          43        Assert.assertEquals(3, result);
          44    }

          45    
          46    public void testMultiply()
          47    {
          48        Calculator cal = new Calculator();
          49        int result = cal.multiply(42);
          50        Assert.assertEquals(8,result);
          51    }

          52    
          53    public void testDivide()
          54    {
          55//        Calculator cal = new Calculator();
          56        int result = 0;
          57        try {
          58            result = cal.divide(10,5);
          59        }
           catch (Exception e) {
          60            e.printStackTrace();
          61            //我們期望result = cal.divide(10,5);正常執(zhí)行;如果進(jìn)入到catch中說明失敗;
          62            //所以我們加上fail。
          63            Assert.fail();//如果這行沒有執(zhí)行。說明這部分正確。
          64        }

          65        Assert.assertEquals(2,result);
          66    }

          67    
          68    public void testDivide2()
          69    {
          70        Throwable tx = null
          71        try 
          72        {
          73//            Calculator cal = new Calculator();
          74            cal.divide(10,0);
          75            //正常來講cal.divide(10,0);已經(jīng)拋出異常,之后的代碼不會(huì)被執(zhí)行。
          76            //我們也期望是這樣的。所以說如果下面的Assert.fail();執(zhí)行了。
          77            //我的的測(cè)試就失敗了。
          78            Assert.fail();//當(dāng)執(zhí)行到這里測(cè)試失敗,后面的代碼將不被執(zhí)行。
          79        }
           catch (Exception e) {
          80            tx = e;
          81        }

          82        Assert.assertNotNull(tx);//斷言tx不為空。也就是說肯定有異常。
          83        Assert.assertEquals(Exception.class,tx.getClass());//斷言tx的類型為Exception類型
          84        Assert.assertEquals("除數(shù)不能為零", tx.getMessage());
          85    }

          86}

          本例中只有一個(gè)測(cè)試類。不過在開發(fā)過程中隨著項(xiàng)目的壯大,測(cè)試類也會(huì)越來越多。我們總不能每次測(cè)試都去挨個(gè)的執(zhí)行每個(gè)test類吧。所以我們要寫個(gè)總體的test類:
          代碼如下:
           1package com.test.junit;
           2
           3import junit.framework.Test;
           4import junit.framework.TestCase;
           5import junit.framework.TestSuite;
           6
           7public class TestAll extends TestCase {
           8    public static Test suite()
           9    {
          10        TestSuite suite = new TestSuite();
          11         suite.addTestSuite(CalculatorTest.class);//加入你所想測(cè)試的測(cè)試類
          12         
          13         return suite;
          14    }

          15}

          posted on 2008-11-25 17:22 生命的綻放 閱讀(5342) 評(píng)論(1)  編輯  收藏 所屬分類: JUnit

          評(píng)論

          # re: Junit簡(jiǎn)單例子[未登錄] 2012-07-14 09:32 啊啊

          共 產(chǎn) 黨  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2008年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(5)

          隨筆分類(94)

          隨筆檔案(93)

          文章分類(5)

          文章檔案(5)

          相冊(cè)

          JAVA之橋

          SQL之音

          兄弟之窗

          常用工具下載

          積分與排名

          最新評(píng)論

          閱讀排行榜

          主站蜘蛛池模板: 高阳县| 英吉沙县| 赞皇县| 临邑县| 金乡县| 巨鹿县| 苍梧县| 海晏县| 安新县| 通辽市| 曲周县| 南宫市| 车险| 临夏市| 剑河县| 南部县| 衡阳县| 宁化县| 蛟河市| 泾川县| 甘洛县| 曲松县| 万安县| 满洲里市| 崇义县| 孝义市| 自治县| 高密市| 托克逊县| 虞城县| 石家庄市| 浏阳市| 织金县| 定州市| 海伦市| 金山区| 浮梁县| 巨野县| 彭山县| 大方县| 南雄市|