示例代碼:
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: 判斷字節數組是否相等 */ @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()); }}
一個測試類單元測試的執行順序為:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
@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: 在該類加載時運行,有且僅僅執行一次 */ @BeforeClass public static void testBeforeClass(){ demo=new Demo(); System.out.println("Test --------->testBeforeClass"); } /** * @Title: testBefore * @Description: 在每一個測試執行前,自動被調用 */ @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: 每一個測試執行結束后會被調用 */ @After public void testAfter(){ System.out.println("Test----------->testAfter"); } /** * @Title: testAfterClass * @Description: 所有測試執行結束以后,執行(有且執行一次) */ @AfterClass public static void testAfterClass(){ System.out.println("Test------------>testAfterClass"); }}
有時一個測試方法,不同的參數值會產生不同的結果,那么我們為了測試全面,會把多個參數值都寫出來并一一斷言測試,這樣有時難免費時費力,這是我們便可以采用參數化測試來解決這個問題。參數化測試就好比把一個“輸入值,期望值”的集合傳入給測試方法,達到一次性測試的目的。
示例代碼:
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: 多組數據的單元測試(參數化測試)* @author lky * @date 2015年10月17日 上午9:20:54 * @version V1.0 *///不使用junit默認的運行器,指定特定的運行器@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: 測試數據初始化 * @param @return 設定文件 * @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)); } }
示例代碼:
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 { //定義被測試方法的時間參數, @Rule public Timeout timeout=new Timeout(10000); @Test public void test(){ } @Test(timeout=10000) public void test1(){ }}
如果一個項目中有很多個測試用例,如果一個個測試也很麻煩,因此打包測試就是一次性測試完成包中含有的所有測試用例。
示例代碼:
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 {}
學習
新聞熱點
疑難解答