国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Junit學(xué)習(xí)

2019-11-14 15:25:07
字體:
供稿:網(wǎng)友

一. 斷言核心方法

360截圖-6668262

示例代碼:

package com.test;import org.junit.Assert;import org.junit.Test;/*** @Title: testAssert.java * @Package com.test * @Description: junit中常見的斷言* @author lky * @date 2015年10月17日 上午9:15:59 * @version V1.0 */public class testAssert {        /**    * @Title: testAssertByteArrayEqual     * @Description: 判斷字節(jié)數(shù)組是否相等      */    @Test    public void testAssertByteArrayEqual(){        Assert.assertArrayEquals("byteArray fail to equal ", "lky".getBytes(),"lky".getBytes());    }        /**    * @Title: testAssertEqual     * @Description: 判斷兩個對象是否相等,只比較值,不比較它們的地址,類似于java中的equal的比較     */    @Test    public void testAssertEqual(){        Assert.assertEquals("fail to equql", 50,50);    }        @Test    public void testAssertNotEqual(){        Assert.assertNotEquals("should be not equal",50,49);    }        /**    * @Title: testAssertNotNull     * @Description: 判斷一個對象是否為空     */    @Test    public void testAssertNotNull(){        Assert.assertNotNull("should be not null", new Object());    }        @Test    public void testAssertNull(){        Assert.assertNull("should be null", null);    }        /**    * @Title: testAssertSame     * @Description: 判斷兩個對象是否相等,包括值和地址,類似于java中的=     */    @Test    public void testAssertSame(){        Integer number=Integer.valueOf(10);        Assert.assertSame("should be same",number ,number);    }    @Test    public void testAssertNotSame(){        Assert.assertNotSame("should be not same", new Object(), new Object());    }}

二.注解核心方法

360截圖-6719212

  • 執(zhí)行順序

一個測試類單元測試的執(zhí)行順序?yàn)椋?/font>

@BeforeClass –> @Before –> @Test –> @After –> @AfterClass

  • 每一個測試方法的調(diào)用順序?yàn)椋?/font>

@Before –> @Test –> @After

示例代碼:

package com.test;import org.junit.After;import org.junit.AfterClass;import org.junit.Assert;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Ignore;import org.junit.Test;/*** @Title: testAnnotation.java * @Package com.test * @Description: junit測試中注解測試* @author lky * @date 2015年10月17日 上午9:36:39 * @version V1.0 */public class testAnnotation {    PRivate static Demo demo=null;        /**    * @Title: testBeforeClass     * @Description: 在該類加載時運(yùn)行,有且僅僅執(zhí)行一次     */    @BeforeClass    public static void testBeforeClass(){        demo=new Demo();        System.out.println("Test --------->testBeforeClass");    }        /**    * @Title: testBefore     * @Description: 在每一個測試執(zhí)行前,自動被調(diào)用     */    @Before    public void testBefore(){        System.out.println("Test---------->testBefore");    }        /**    * @Title: testAdd     * @Description: 真正去做測試的代碼     */    @Test    public void testAdd(){        Assert.assertEquals(5, demo.add(2, 3));    }        /**    * @Title: testAdd1     * @Description: ignore表示忽略該測試     */    @Ignore    @Test    public void testAdd1(){         Assert.assertEquals(4, demo.add(2, 2));    }        /**    * @Title: testAfter     * @Description: 每一個測試執(zhí)行結(jié)束后會被調(diào)用     */    @After    public void testAfter(){        System.out.println("Test----------->testAfter");    }        /**    * @Title: testAfterClass     * @Description: 所有測試執(zhí)行結(jié)束以后,執(zhí)行(有且執(zhí)行一次)     */    @AfterClass    public static void testAfterClass(){        System.out.println("Test------------>testAfterClass");    }}

三.參數(shù)化測試

     有時一個測試方法,不同的參數(shù)值會產(chǎn)生不同的結(jié)果,那么我們?yōu)榱藴y試全面,會把多個參數(shù)值都寫出來并一一斷言測試,這樣有時難免費(fèi)時費(fèi)力,這是我們便可以采用參數(shù)化測試來解決這個問題。參數(shù)化測試就好比把一個“輸入值,期望值”的集合傳入給測試方法,達(dá)到一次性測試的目的。

示例代碼:

package com.test;public class Demo {        public int add(int a,int b){        return a+b;    }}
package com.test;import java.util.Arrays;import java.util.Collection;import org.junit.Assert;import org.junit.BeforeClass;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import org.junit.runners.Parameterized.Parameters;/*** @Title: testParam.java * @Package com.test * @Description: 多組數(shù)據(jù)的單元測試(參數(shù)化測試)* @author lky * @date 2015年10月17日 上午9:20:54 * @version V1.0 *///不使用junit默認(rèn)的運(yùn)行器,指定特定的運(yùn)行器@RunWith(Parameterized.class)public class testParam {    private int expected;    private int input1;    private int input2;    private static  Demo demo;        public testParam(int expected,int input1,int input2) {        this.expected=expected;        this.input1=input1;        this.input2=input2;    }        /**    * @Title: initData     * @Description: 測試數(shù)據(jù)初始化    * @param @return    設(shè)定文件     * @return Collection<? extends Object>    返回類型     * @throws     */    @Parameters(name="第 {index} 組:-------> {1} + {2} = {0}")    public static Collection<?extends Object> initData(){        return Arrays.asList(new Object [][]{{3,2,1},{5,-1,6},{-7,-3,-4},{7,3,4}});    }        @BeforeClass    public static void loadUp(){        demo=new Demo();    }        @Test    public void testAdd(){        Assert.assertEquals("should be equal", this.expected, demo.add(this.input1, this.input2));    }}

四.異常測試

示例代碼:

package com.test;import java.util.ArrayList;import java.util.List;import org.junit.Assert;import org.junit.Rule;import org.junit.Test;import org.junit.rules.ExpectedException;/** * * @Title: testException.java * @Package com.test * @Description: 異常測試(兩種方法)* @author lky * @date 2015年10月17日 上午10:00:32 * @version V1.0 */public class testException {        @Test(expected=IndexOutOfBoundsException.class)    public void empty(){        new ArrayList<Object>().get(0);    }            @Rule    public ExpectedException thrown=ExpectedException.none();        @Test      public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {          List<?extends Object> list=new ArrayList<Object>();        thrown.expectMessage("Index: 0, Size: 0");        thrown.expect(IndexOutOfBoundsException.class);        list.get(0);        Assert.assertEquals(1, list.get(0));    }                  }

五.超時測試

         有時為了防止出現(xiàn)死循環(huán)或者方法執(zhí)行過長(或檢查方法效率),而需要使用到限時測試。顧名思義,就是超出設(shè)定時間即視為測試失敗。共有兩種寫法

示例代碼:

package com.test;import org.junit.Rule;import org.junit.Test;import org.junit.rules.Timeout;/*** @Title: testTimeOut.java * @Package com.test * @Description: 超時測試* @author lky * @date 2015年10月17日 上午9:58:18 * @version V1.0 */public class testTimeOut {        //定義被測試方法的時間參數(shù),    @Rule    public Timeout timeout=new Timeout(10000);    @Test    public void test(){                    }        @Test(timeout=10000)    public void test1(){                    }}

六.打包測試

       如果一個項(xiàng)目中有很多個測試用例,如果一個個測試也很麻煩,因此打包測試就是一次性測試完成包中含有的所有測試用例。

示例代碼:

package com.test;import org.junit.runner.RunWith;import org.junit.runners.Suite;import org.junit.runners.Suite.SuiteClasses;/*** @Title: testSuite.java * @Package com.test * @Description: 打包測試* @author lky * @date 2015年10月17日 上午10:05:25 * @version V1.0 */@RunWith(Suite.class)@SuiteClasses({testAnnotation.class,testAssert.class,testParam.class,testException.class,testTimeOut.class})public class testSuite {}

七.Spring框架中的使用

學(xué)習(xí)


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 广元市| 昌图县| 中方县| 滁州市| 平山县| 柞水县| 叙永县| 丰镇市| 高清| 邵东县| 阜新市| 靖安县| 九龙县| 宁南县| 建始县| 扶风县| 阳东县| 上饶市| 霍城县| 广东省| 北票市| 望城县| 广河县| 北海市| 越西县| 民乐县| 宁阳县| 肇源县| 晋宁县| 全南县| 利津县| 定襄县| 来安县| 安图县| 娱乐| 清原| 灌阳县| 湟源县| 滦平县| 延吉市| 芒康县|