亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
JUnit介紹及其快速使用
作者:網絡轉載 發布時間:[ 2013/3/19 15:03:46 ] 推薦標簽:

  現在已經了解了JUnit3.8.1的使用和其基本的工作原理,JUnit 4是JUnit框架有史以來的大改進,其主要目標便是利用Java 5的Annotation特性簡化測試用例的編寫。下面同樣通過代碼來學習一下:

  1.右擊該類,選擇 新建->JUnit測試用例,選擇JUnit4,setUp和tearDown方法,點擊下一步,選擇需要測試的方法,JUnit會自動生成測試的代碼框架(可以看到這個代碼框架和上面的有較大的不同),手動添加自己的測試代碼后如下:

 
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class HelloTest4 {        //這里不需要繼承自TestCase

    private Hello hello;
    public HelloTest4()
    {
        super();
        System.out.println("a new test instance...");
    }
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("call before all tests...");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("call after all tests... ");
    }
    @Before
    public void setUp() throws Exception {
        //這里不需要調用super.setUp()
        System.out.println("call before test...");
        hello=new Hello();
    }

    @After
    public void tearDown() throws Exception {
        //這里不需要調用super.tearDown()
        System.out.println("call after test... ");
    }

    @Test
    public void testAbs() {
        System.out.println("test the method abs()");
        assertEquals(16, hello.abs(16));
        assertEquals(11, hello.abs(-10));//在這里,會出現故障,應該把左邊的參數改為10
        assertEquals(0, hello.abs(0));
    }

    @Test
    public void testDivision() {
        System.out.println("test the method division()");
        assertEquals(3D, hello.division(6, 2));
        assertEquals(6D, hello.division(6, 1));
        assertEquals(0D, hello.division(6, 0));//在這里,會出現故障(與3.8.1有些不同?)
    }

    //下面,并不是對JunitDemo類中成員函數的測試,只是演示JUnit的一些功能
   
    //測試是否會發生期望的異常
    @Test(expected=ArithmeticException.class)
    public void testDiv0() {
        System.out.println("test the method Div0()");
        double result=100/0;
    }
   
    //測試是否超時
    @Test(timeout=1)
    public void testLongTimeTask()
    {
        System.out.println("test the method LongTimeTask()");
        double d = 0;
        for(int i=1; i<10000000; i++)
            d+=i;
    }
   
}

  2.運行該測試類,輸出如下:

call before all tests...
a new test instance...
call before test...
test the method abs()
call after test...

a new test instance...
call before test...
test the method division()
call after test...

a new test instance...
call before test...
test the method Div0()
call after test...

a new test instance...
call before test...
test the method LongTimeTask()
call after test...

call after all tests...
  3.從上面的輸出結果可以看出,JUnit的工作原理和以前的幾乎還是沒有變的,只是讓用戶使用更簡單了當然有一個變化是3.8.1的所有測試實例是測試前全都創建好的,而JUnit4的測試實例是在每個測試前創建的。

  4.當JUnit4還有一個與以前很大的不同是引入了@BeforeClass和@AfterClass(可以在選擇setUp和tearDown的時候選擇setUpBeforeClass()和tearDownAfterClass()),setUpBeforeClass()在所有測試前調用,tearDownAfterClass()在所有測試后調用,它們不同與setUp和tearDown,在整個測試過程中只會被調用一次,這是為了能在@BeforeClass中初始化一些昂貴的資源,例如數據庫連接,然后執行所有的測試方法,后在@AfterClass中釋放資源。

  總結,這里只是對JUnit對class的單元測試作了簡單的討論,除此以外,JUnit還可以對JSP,Servlt,EJB等做單元測試。

上一頁12下一頁
軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 国内精品视频在线播放 | 色综合久久久高清综合久久久 | 色草视频| 欧美三级大片在线观看 | 天天视频色版 | 久中文字幕中文字幕亚洲无线 | 日韩一区二区三区不卡 | 综合激情网五月 | 全免费毛片在线播放 | 亚洲女女同志videos | 日韩午夜视频在线观看 | 五月天婷婷激情 | 日本在线亚州精品视频在线 | 国产日日干 | 亚洲成人影院在线 | 日日日操 | 国产人成久久久精品 | 欧美日韩在线国产 | 亚洲经典在线中文字幕 | 丁香六月婷婷综合 | 波多野结衣久久精品 | 深夜国产成人福利在线观看女同 | 黑人最猛性free护士hd | 免费在线观看中文字幕 | 日本免费全黄一级裸片视频 | 在线看的你懂的 | 亚洲欧美日韩精品在线 | 香蕉成人国产精品免费看网站 | 亚洲欧美日韩中文高清一 | 一级特黄色毛片免费看 | 九九九热在线精品免费全部 | 欧美日韩国产手机在线观看视频 | 日韩小视频在线 | 国产超91| 五月天婷婷网站 | 上色天天综合网 | 成人毛片视频免费网站观看 | 精品在线99| 在线欧美日韩国产 | 久久精品人人爽人人爽 | 日韩黄漫啪啪免费动漫 |