一. 断言核心方法
示例代码:
packagecom.test;importorg.junit.Assert;importorg.junit.Test;/*** @Title: testAssert.java* @Package com.test
* @Description: junit中常见的断言
*@authorlky
* @date 2015年10月17日 上午9:15:59
*@versionV1.0*/publicclasstestAssert {/*** @Title: testAssertByteArrayEqual
* @Description: 判断字节数组是否相等*/@TestpublicvoidtestAssertByteArrayEqual(){
Assert.assertArrayEquals("byteArray fail to equal ", "lky".getBytes(),"lky".getBytes());
}/*** @Title: testAssertEqual
* @Description: 判断两个对象是否相等,只比较值,不比较它们的地址,类似于java中的equal的比较*/@TestpublicvoidtestAssertEqual(){
Assert.assertEquals("fail to equql", 50,50);
}
@TestpublicvoidtestAssertNotEqual(){
Assert.assertNotEquals("should be not equal",50,49);
}/*** @Title: testAssertNotNull
* @Description: 判断一个对象是否为空*/@TestpublicvoidtestAssertNotNull(){
Assert.assertNotNull("should be not null",newObject());
}
@TestpublicvoidtestAssertNull(){
Assert.assertNull("should be null",null);
}/*** @Title: testAssertSame
* @Description: 判断两个对象是否相等,包括值和地址,类似于java中的=*/@TestpublicvoidtestAssertSame(){
Integer number=Integer.valueOf(10);
Assert.assertSame("should be same",number ,number);
}
@TestpublicvoidtestAssertNotSame(){
Assert.assertNotSame("should be not same",newObject(),newObject());
}
}
二.注解核心方法
执行顺序一个测试类单元测试的执行顺序为:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一个测试方法的调用顺序为:@Before –> @Test –> @After
示例代码:
packagecom.test;importorg.junit.After;importorg.junit.AfterClass;importorg.junit.Assert;importorg.junit.Before;importorg.junit.BeforeClass;importorg.junit.Ignore;importorg.junit.Test;/*** @Title: testAnnotation.java
* @Package com.test
* @Description: junit测试中注解测试
*@authorlky
* @date 2015年10月17日 上午9:36:39
*@versionV1.0*/publicclasstestAnnotation {PRivatestaticDemo demo=null;/*** @Title: testBeforeClass
* @Description: 在该类加载时运行,有且仅仅执行一次*/@BeforeClasspublicstaticvoidtestBeforeClass(){
demo=newDemo();
System.out.println("Test --------->testBeforeClass");
}/*** @Title: testBefore
* @Description: 在每一个测试执行前,自动被调用*/@BeforepublicvoidtestBefore(){
System.out.println("Test---------->testBefore");
}/*** @Title: testAdd
* @Description: 真正去做测试的代码*/@TestpublicvoidtestAdd(){
Assert.assertEquals(5, demo.add(2, 3));
}/*** @Title: testAdd1
* @Description: ignore表示忽略该测试*/@Ignore
@TestpublicvoidtestAdd1(){
Assert.assertEquals(4, demo.add(2, 2));
}/*** @Title: testAfter
* @Description: 每一个测试执行结束后会被调用*/@AfterpublicvoidtestAfter(){
System.out.println("Test----------->testAfter");
}/*** @Title: testAfterClass
* @Description: 所有测试执行结束以后,执行(有且执行一次)*/@AfterClasspublicstaticvoidtestAfterClass(){
System.out.println("Test------------>testAfterClass");
}
}
三.参数化测试 有时一个测试方法,不同的参数值会产生不同的结果,那么我们为了测试全面,会把多个参数值都写出来并一一断言测试,这样有时难免费时费力,这是我们便可以采用参数化测试来解决这个问题。参数化测试就好比把一个“输入值,期望值”的集合传入给测试方法,达到一次性测试的目的。示例代码:
packagecom.test;publicclassDemo {publicintadd(inta,intb){returna+b;
}
}
packagecom.test;importjava.util.Arrays;importjava.util.Collection;importorg.junit.Assert;importorg.junit.BeforeClass;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.junit.runners.Parameterized;importorg.junit.runners.Parameterized.Parameters;/*** @Title: testParam.java
* @Package com.test
* @Description: 多组数据的单元测试(参数化测试)
*@authorlky
* @date 2015年10月17日 上午9:20:54
*@versionV1.0*///不使用junit默认的运行器,指定特定的运行器@RunWith(Parameterized.class)publicclasstestParam {privateintexpected;privateintinput1;privateintinput2;privatestaticDemo demo;publictestParam(intexpected,intinput1,intinput2) {this.expected=expected;this.input1=input1;this.input2=input2;
}/*** @Title: initData
* @Description: 测试数据初始化
*@param@return设定文件
*@returnCollection<? extends Object> 返回类型
*@throws*/@Parameters(name="第 {index} 组:-------> {1} + {2} = {0}")publicstaticCollection<?extendsObject>initData(){returnArrays.asList(newObject [][]{{3,2,1},{5,-1,6},{-7,-3,-4},{7,3,4}});
}
@BeforeClasspublicstaticvoidloadUp(){
demo=newDemo();
}
@TestpublicvoidtestAdd(){
Assert.assertEquals("should be equal",this.expected, demo.add(this.input1,this.input2));
}
}
四.异常测试示例代码:
packagecom.test;importjava.util.ArrayList;importjava.util.List;importorg.junit.Assert;importorg.junit.Rule;importorg.junit.Test;importorg.junit.rules.ExpectedException;/***
* @Title: testException.java
* @Package com.test
* @Description: 异常测试(两种方法)
*@authorlky
* @date 2015年10月17日 上午10:00:32
*@versionV1.0*/publicclasstestException {
@Test(expected=IndexOutOfBoundsException.class)publicvoidempty(){newArrayList<Object>().get(0);
}
@RulepublicExpectedException thrown=ExpectedException.none();
@TestpublicvoidshouldTestExceptionMessage()throwsIndexOutOfBoundsException {
List<?extendsObject> list=newArrayList<Object>();
thrown.expectMessage("Index: 0, Size: 0");
thrown.expect(IndexOutOfBoundsException.class);
list.get(0);
Assert.assertEquals(1, list.get(0));
}
}
五.超时测试 有时为了防止出现死循环或者方法执行过长(或检查方法效率),而需要使用到限时测试。顾名思义,就是超出设定时间即视为测试失败。共有两种写法示例代码:
packagecom.test;importorg.junit.Rule;importorg.junit.Test;importorg.junit.rules.Timeout;/*** @Title: testTimeOut.java
* @Package com.test
* @Description: 超时测试
*@authorlky
* @date 2015年10月17日 上午9:58:18
*@versionV1.0*/publicclasstestTimeOut {//定义被测试方法的时间参数,@RulepublicTimeout timeout=newTimeout(10000);
@Testpublicvoidtest(){
}
@Test(timeout=10000)publicvoidtest1(){
}
}
六.打包测试 如果一个项目中有很多个测试用例,如果一个个测试也很麻烦,因此打包测试就是一次性测试完成包中含有的所有测试用例。
示例代码:
packagecom.test;importorg.junit.runner.RunWith;importorg.junit.runners.Suite;importorg.junit.runners.Suite.SuiteClasses;/*** @Title: testSuite.java
* @Package com.test
* @Description: 打包测试
*@authorlky
* @date 2015年10月17日 上午10:05:25
*@versionV1.0*/@RunWith(Suite.class)
@SuiteClasses({testAnnotation.class,testAssert.class,testParam.class,testException.class,testTimeOut.class})publicclasstestSuite {
}
七.Spring框架中的使用学习