免费高清特黄a大片,九一h片在线免费看,a免费国产一级特黄aa大,国产精品国产主播在线观看,成人精品一区久久久久,一级特黄aa大片,俄罗斯无遮挡一级毛片

分享

用JUnit4進(jìn)行單元測(cè)試

 hehffyy 2016-09-06

用Junit進(jìn)行單元測(cè)試很方便,尤其是Junit4引入了很多Annotation注解以后。看測(cè)試的示例:

待測(cè)試類:

Java代碼  收藏代碼
  1. public class Calculator {  
  2.   
  3.     public int add(int a, int b) {  
  4.         return a + b;  
  5.     }  
  6.   
  7.     public int minus(int a, int b) {  
  8.         return a - b;  
  9.     }  
  10.   
  11.     public int square(int n) {  
  12.         return n * n;  
  13.     }  
  14.       
  15.     //Bug : 死循環(huán)  
  16.     public void squareRoot(int n) {  
  17.         for(; ;)  
  18.             ;  
  19.     }  
  20.       
  21.     public int multiply(int a, int b) {  
  22.         return a * b;  
  23.     }  
  24.   
  25.     public int divide(int a, int b) throws Exception {  
  26.         if (0 == b) {  
  27.             throw new Exception("除數(shù)不能為零");  
  28.         }  
  29.         return a / b;  
  30.     }  
  31. }  

 

 單元測(cè)試類:

Java代碼  收藏代碼
  1. import org.junit.After;  
  2. import org.junit.AfterClass;  
  3. import org.junit.Assert;  
  4. import org.junit.Before;  
  5. import org.junit.BeforeClass;  
  6. import org.junit.Ignore;  
  7. import org.junit.Test;  
  8.   
  9. public class CalculatorTest {  
  10.   
  11.     private Calculator cal = new Calculator();  
  12.   
  13.     @BeforeClass // 注意,這里必須是static...因?yàn)榉椒▽⒃陬惐谎b載的時(shí)候就被調(diào)用(那時(shí)候還沒創(chuàng)建實(shí)例)  
  14.     public static void before()  
  15.     {  
  16.         System.out.println("global");  
  17.     }  
  18.   
  19.     @AfterClass  
  20.     public static void after() {  
  21.         System.out.println("global destroy");  
  22.     }  
  23.   
  24.     @Before  
  25.     public void setUp() throws Exception {  
  26.         System.out.println("一個(gè)測(cè)試開始。。");  
  27.     }  
  28.   
  29.     @After  
  30.     public void tearDown() throws Exception {  
  31.         System.out.println("一個(gè)測(cè)試結(jié)束");  
  32.     }  
  33.   
  34.     @Test  
  35.     @Ignore  
  36.     public void testAdd() {  
  37.         int result = cal.add(12);  
  38.         Assert.assertEquals(3, result);  
  39.     }  
  40.   
  41.     @Test  
  42.     public void testMinus() {  
  43.         int result = cal.minus(52);  
  44.         Assert.assertEquals(3, result);  
  45.     }  
  46.   
  47.     @Test  
  48.     public void testMultiply() {  
  49.         int result = cal.multiply(42);  
  50.         Assert.assertEquals(8, result);  
  51.     }  
  52.   
  53.     @Test(timeout = 1000// 單位為毫秒  
  54.     public void testSquareRoot() {  
  55.         cal.squareRoot(4);  
  56.     }  
  57.   
  58.     @Test(expected = Exception.class)  
  59.     public void testDivide() throws Exception {  
  60.         System.out.println("teddd");  
  61.         cal.divide(40);// 很簡(jiǎn)單的辦法.......  
  62.     }  
  63.   
  64. //  @Test  
  65. //  public void testDivide() {  
  66. //      int result = 0;  
  67. //      try {  
  68. //          result = cal.divide(10, 5);  
  69. //      } catch (Exception e) {  
  70. //          e.printStackTrace();  
  71. //          Assert.fail();// 如果這行沒有執(zhí)行。說明這部分正確。  
  72. //      }  
  73. //      Assert.assertEquals(2, result);  
  74. //  }  
  75. }  

 在Eclipse里Run As -> JUnit Test,運(yùn)行測(cè)試類,Eclipse的JUnit的View顯示如:

        可以看到,CalculatorTest類中總共有5個(gè)測(cè)試用例,ignore了一個(gè),3個(gè)測(cè)試用例通過,testSquareRoot測(cè)試不通過(因?yàn)槌瑫r(shí)),所以整個(gè)的測(cè)試結(jié)果飄紅了。同時(shí),控制臺(tái)的輸出結(jié)果為:

Txt代碼  收藏代碼
  1. global  
  2. 一個(gè)測(cè)試開始。。  
  3. 一個(gè)測(cè)試結(jié)束  
  4. 一個(gè)測(cè)試開始。。  
  5. 一個(gè)測(cè)試結(jié)束  
  6. 一個(gè)測(cè)試開始。。  
  7. 一個(gè)測(cè)試結(jié)束  
  8. 一個(gè)測(cè)試開始。。  
  9. teddd  
  10. 一個(gè)測(cè)試結(jié)束  
  11. global destroy  

各種注解的說明:

@Test:

表明該方法是一個(gè)測(cè)試方法

 

@BeforeClass 和 @AfterClass:

測(cè)試用例初始化時(shí)執(zhí)行 @BeforeClass方法,當(dāng)所有測(cè)試執(zhí)行完畢之后,執(zhí)行@AfterClass進(jìn)行收尾工作。標(biāo)注、@BeforeClass 和 @AfterClass的方法必須是static的,因?yàn)榉椒▽⒃陬惐谎b載的時(shí)候就被調(diào)用,那時(shí)候還沒創(chuàng)建測(cè)試對(duì)象實(shí)例。

 

@Before: 
使用了該元數(shù)據(jù)的方法在每個(gè)測(cè)試方法執(zhí)行之前都要執(zhí)行一次。

@After: 
使用了該元數(shù)據(jù)的方法在每個(gè)測(cè)試方法執(zhí)行之后要執(zhí)行一次。

 

@Test(expected=*.class) :
通過@Test元數(shù)據(jù)中的expected屬性驗(yàn)證是否拋出期望的異常,expected屬性的值是一個(gè)異常的類型,如果拋出了期望的異常,則測(cè)試通過,否則不通過。

 

@Test(timeout=xxx):
該元數(shù)據(jù)傳入了一個(gè)時(shí)間(毫秒)給測(cè)試方法,如果測(cè)試方法在制定的時(shí)間之內(nèi)沒有運(yùn)行完,則測(cè)試也失敗。

 

@Ignore: 
該元數(shù)據(jù)標(biāo)記的測(cè)試方法在測(cè)試中會(huì)被忽略。同時(shí)可以為該標(biāo)簽傳遞一個(gè)String的參數(shù),來表明為什么會(huì)忽略這個(gè)測(cè)試方法。比如:@lgnore("該方法還沒有實(shí)現(xiàn)"),在執(zhí)行的時(shí)候,僅會(huì)報(bào)告該方法沒有實(shí)現(xiàn),而不會(huì)運(yùn)行測(cè)試方法。

 

在test方法內(nèi)除了使用Assert的assertEquals()方法外,還能使用assertFalse()、assertTrue()、assertNull()、assertNotNull()、assertSame()、assertNotSame()等斷言函數(shù)。而且如果使用的是Junit4,結(jié)合Hamcrest,使用

assertThat([value], [matcher statement])方法可以實(shí)現(xiàn)更靈活的斷言判斷(前提是引入hamcrest的jar包)。

例如:

// is匹配符表明如果前面待測(cè)的object等于后面給出的object,則測(cè)試通過 

assertThat( testedObj, is( object) ); 

 

// containsString匹配符表明如果測(cè)試的字符串包含指定的子字符串則測(cè)試通過

 assertThat( testedString, containsString( "developerWorks" ) );

 

// greaterThan匹配符表明如果所測(cè)試的數(shù)值testedNumber大于16.0則測(cè)試通過

 assertThat( testedNumber, greaterThan(16.0) ); 

 

// closeTo匹配符表明如果所測(cè)試的浮點(diǎn)型數(shù)testedDouble在20.0±0.5范圍之內(nèi)則測(cè)試通過 

assertThat( testedDouble, closeTo( 20.0, 0.5 ) );

 

//hasItem匹配符表明被測(cè)的迭代對(duì)象含有元素element項(xiàng)則測(cè)試通過assertThat(iterableObject, hasItem (element));

 

更多更詳細(xì)的Hamcrest提供的斷言判斷參考:

http://hi.baidu.com/shenhuanyu09/item/2bcfcb981aa3188e581461b4

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////分割線

 

       上面的Caculator待測(cè)試類里,現(xiàn)在我如果想給square方法多弄幾個(gè)測(cè)試用例,按照上面的方法,我應(yīng)該寫好幾個(gè)@Test方法來測(cè)試,或者每次測(cè)完再改一下輸入的值和期望得到的值,好麻煩。JUnit提供如下的測(cè)試:

Java代碼  收藏代碼
  1. import java.util.Arrays;  
  2. import java.util.Collection;  
  3.   
  4. import org.junit.Assert;  
  5. import org.junit.Test;  
  6. import org.junit.runner.RunWith;  
  7. import org.junit.runners.Parameterized;  
  8. import org.junit.runners.Parameterized.Parameters;  
  9.   
  10. @RunWith(Parameterized.class)  
  11. public class CalculatorTest2{  
  12.   
  13.     private Calculator cal = new Calculator();  
  14.     private int param;  
  15.     private int result;  
  16.   
  17.     //構(gòu)造函數(shù),對(duì)變量進(jìn)行初始化  
  18.     //定義一個(gè)待測(cè)試的類,并且定義兩個(gè)變量,一個(gè)用于存放參數(shù),一個(gè)用于存放期待的結(jié)果。  
  19.     public CalculatorTest2(int param, int result) {  
  20.            this.param = param;  
  21.            this.result = result;  
  22.     }  
  23.       
  24.     @Parameters  
  25.     public static Collection data(){  
  26.         return Arrays.asList(new Object[][]{  
  27.             {24},  
  28.             {00},  
  29.             {-39},  
  30.       });  
  31.     }  
  32.   
  33.     @Test  
  34.     public void squareTest() {  
  35.         int temp = cal.square(param);  
  36.         Assert.assertEquals(result, temp);  
  37.     }  
  38. }  

 Eclipse里JUnit的運(yùn)行結(jié)果顯示為:

       測(cè)試通過了,CalculatorTest2類里的parameter是每次的測(cè)試輸入,result就是測(cè)試的結(jié)果。所有的測(cè)試輸入和期望結(jié)果都在@Parameters標(biāo)注的data函數(shù)的返回的Collection集合里,2的期望得到的平方結(jié)果值是4,0期望得到0,-3期望得到9。

 

       把測(cè)試代碼提交給JUnit框架后,框架如何來運(yùn)行代碼呢?答案就是——Runner。在JUnit中有很多個(gè) Runner,他們負(fù)責(zé)調(diào)用測(cè)試代碼,每一個(gè)Runner都有各自的特殊功能,要根據(jù)需要選擇不同的Runner來運(yùn)行測(cè)試代碼。JUnit中有一個(gè)默認(rèn)Runner,如果沒有指定,那么系統(tǒng)自動(dòng)使用默認(rèn) Runner來運(yùn)行你的代碼。這里參數(shù)化測(cè)試就沒有再用默認(rèn)的Runner了。

 

再看看打包測(cè)試測(cè)例子:

Java代碼  收藏代碼
  1. import org.junit.runner.RunWith;  
  2. import org.junit.runners.Suite;  
  3.   
  4. /** 
  5.  * 大家可以看到,這個(gè)功能也需要使用一個(gè)特殊的Runner, 
  6.  * 因此我們需要向@RunWith標(biāo)注傳遞一個(gè)參數(shù)Suite.class。 
  7.  * 同時(shí),我們還需要另外一個(gè)標(biāo)注@Suite.SuiteClasses, 
  8.  * 來表明這個(gè)類是一個(gè)打包測(cè)試類。我們把需要打包的類作為參數(shù)傳遞給該標(biāo)注就可以了。 
  9.  * 有了這兩個(gè)標(biāo)注之后,就已經(jīng)完整的表達(dá)了所有的含義,因此下面的類已經(jīng)無關(guān)緊要, 
  10.  * 隨便起一個(gè)類名,內(nèi)容全部為空既可 
  11.  * 
  12.  */  
  13. @RunWith(Suite.class)  
  14. @Suite.SuiteClasses({ CalculatorTest.class, CalculatorTest2.class })  
  15. public class AllCalculatorTests {  
  16.   
  17. }  

       這個(gè)測(cè)試類包含了上面的CalculatorTest.class和CalculatorTest2.class里面所有的測(cè)試函數(shù),它的目的就是進(jìn)行打包所有的測(cè)試。

 

參考:

http://blog.csdn.net/zhuangxiu/article/details/6256893

http://blog.csdn.net/songyinan1989/article/details/7445921

http://www.jb51.net/article/15797.htm

http://jingbo2759.blog.163.com/blog/static/983753152009711103710146/

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多