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

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

  現(xiàn)在已經(jīng)了解了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 {
        //這里不需要調(diào)用super.setUp()
        System.out.println("call before test...");
        hello=new Hello();
    }

    @After
    public void tearDown() throws Exception {
        //這里不需要調(diào)用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));//在這里,會出現(xiàn)故障,應(yīng)該把左邊的參數(shù)改為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));//在這里,會出現(xiàn)故障(與3.8.1有些不同?)
    }

    //下面,并不是對JunitDemo類中成員函數(shù)的測試,只是演示JUnit的一些功能
   
    //測試是否會發(fā)生期望的異常
    @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.從上面的輸出結(jié)果可以看出,JUnit的工作原理和以前的幾乎還是沒有變的,只是讓用戶使用更簡單了當然有一個變化是3.8.1的所有測試實例是測試前全都創(chuàng)建好的,而JUnit4的測試實例是在每個測試前創(chuàng)建的。

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

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

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 日日干日日射 | 免费观看一级特黄三大片视频 | 日韩av成人| 日韩亚洲国产综合久久久 | 18成人免费观看网站入口 | 国产旗袍丝袜在线观看视频 | 成人短视频在线在线观看 | 精品综合久久久久久99 | 亚洲免费网站观看视频 | 男女爽爽无遮拦午夜视频 | 欧美性4khd| 一级国产a级a毛片无卡 | 妞干网免费看 | 日韩精品成人 | 看全色黄大色黄大片免责看的 | 男女午夜性爽快免费视频不卡 | 在线播放影院 | 国产成人成人一区二区 | 久青草视频在线 | 成人看片软件 | 黄 片影 院 | 久久久久无码国产精品一区 | 国产91亚洲精品 | 男人透女人超爽视频免费 | 欧美一级片免费在线观看 | 一级a性色生活片久久毛片 一级a美女毛片 | 精品午夜寂寞影院在线观看 | 欧美日韩性视频 | 亚洲久热| 国产区精品福利在线观看精品 | 国产在线综合一区二区三区 | 午夜影院404| 丝瓜色板 | 最近最新中文字幕高清免费 | 波多野结衣免费视频观看 | 亚洲欧美在线中文字幕不卡 | 男女第一次弄进去的视频 | 黄色一级大片免费看 | 天天摸夜夜摸狠狠摸夜夜摸 | 福利视频网站 | 午夜在线视频观看版 |