| 01 概述 |
JUnit是一個由 Erich Gamma 和 Kent Beck 編寫的一個回歸測試框架(regression testing framework)。Junit測試是白盒測試。JUnit有它自己的JUnit擴展生態(tài)圈。多數(shù)java的開發(fā)環(huán)境都已經(jīng)集成了JUnit作為單元測試的工具。[1]
JUnit是一個開放源代碼的Java測試框架,用于編寫和運行可重復(fù)的測試。他是用于單元測試框架體系xUnit的一個實例(用于java
語言)。它包括以下特性:
1、用于測試期望結(jié)果的斷言(Assertion)
2、用于共享共同測試數(shù)據(jù)的測試工具
3、用于方便的組織和運行測試的測試套件
4、圖形和文本的測試運行器
測試一般分為:單元測試、集成測試(主要看一塊代碼加進去后,系統(tǒng)會不會有問題)、驗收測試和壓力測試。在大點的公司開發(fā)
人員每天上班后,第一件事情就是從svn上把自己負責的代碼checkout下來,然后運行單元測試,如果單元測試通過,那么說明自己的
代碼沒有問題,然后就在代碼塊上修改與添加,完成后再用junit進行測試,測試完成后如果沒有問題,那么就把相應(yīng)的代碼塊提交給
svn上。[2]
| 02 Junit配置 |
環(huán)境:Eclipse 中配置junit,
或手動 添加Junit.jar和hamcrest-core-xx.jar,hamcrest-library-xx.jar (或hamcrest-all-xx.jar)。(建議手動添加,在eclipse中通過前一種方法只引入Junit-xx.jar 和 hamcrest-core-xx.jar)
| 03 Junit3 和 Junit4的區(qū)別 |
| 04 Junit4的幾個常用注解[3] |
Junit 4 的單元測試用例執(zhí)行順序為:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass;
每一個測試方法的調(diào)用順序為:@Before –> @Test –> @After。
--------------------------------------------------------------------
| 05 hamcrest 和 testSuite 介紹[4] |
hamcrest就是專門為增強junit來提供的框架。Junit中,主要使用assert做一些基本的判斷。但很多時候使用assert做判斷,并不方便,如果要判斷某幾個值是否為true或false,這時使用hamcrest來判斷就會方便許多。
在一個項目中,可能有很多的測試類,如果每個測試類都要點擊運行,那么成百上千個類都需要測試,這會是個比較繁重的工作,這時可以使用可以使用junit的jar包中提供的Suite來解決這個問題。
| 06 綜合實例 |
TestCalcuate類
import static org.junit.Assert.*;import static org.hamcrest.Matchers.*;import org.junit.Before;import org.junit.Test;public class TestCalcuate { Calcuate cal; @Before public void setUp(){ cal=new Calcuate(); } @Test public void testAdd(){ int rel=cal.add(22, 33); assertEquals("加法存在問題",rel, 55); } @Test public void testMinus(){ int rel=cal.minus(33, 11); assertEquals("減法存在問題",rel,22); } @Test(expected=ArithmeticException.class) public void testDivid(){ int rel=cal.divide(22, 0); assertEquals("除法存在問題",rel,2); } @Test public void testMul(){ int rel=cal.mul(10, 20); assertEquals("乘法存在問題", rel,200); } @Test(timeout=1000) public void testTime(){ try{ Thread.sleep(200); }catch(Exception e){ e.printStackTrace(); } int rel=cal.mul(10, 20); assertEquals("乘法存在問題", rel,200); } @Test public void testHam(){ assertThat(50, allOf(greaterThan(49),lessThan(60))); assertThat(60, allOf(greaterThan(40),lessThan(90))); assertThat("like.txt", endsWith(".txt")); }}TestA類
import org.junit.*;public class TestA { @Test public void testA(){ System.out.println("testA"); }}TestB類
import org.junit.*;public class TestB { @Test public void testB(){ System.out.println("testB"); }}TestSuite類
import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Suite;import org.junit.runners.Suite.SuiteClasses;@RunWith(Suite.class)@SuiteClasses({TestA.class,TestB.class,TestCalcuate.class})public class TestSuite { @Test public void testSuite(){ System.out.println("testSuite"); }}
| 07 其它問題 |
1、測試原則:[4]
2、用junit在進行單元測試的時候,使用assertThat方法,發(fā)現(xiàn)hamcrest的greaterThan等方法無法識別【說是沒有定義】,而且無法靜態(tài)引用到Matchers類【import static org.hamcrest.Matchers.*;】 [5]
原因:因為junit默認只會依賴hamcrest-core-1.3包,而實際上hamcrest還有一個hamcrest-library.jar用來放置很多工具
| 參考文獻 |
[1] 百度百科 Junit
[2] 悔海 http://huihai.VEvb.com/blog/1986568
[3] 小倩的博客 http://blog.sina.com.cn/s/blog_8354dda80101ee8v.html
[4] 悔海 http://huihai.VEvb.com/blog/1994270
[5] http://www.fwqtg.net/junit-使用hamcrest的matcher找不到的問題(比如greaterthan).html
新聞熱點
疑難解答