Junit单元测试11111111111Word文件下载.docx
《Junit单元测试11111111111Word文件下载.docx》由会员分享,可在线阅读,更多相关《Junit单元测试11111111111Word文件下载.docx(13页珍藏版)》请在冰豆网上搜索。
而JUnit4类则不需要继承任何类,但在方法前须加上注解@Test(org.junit.Test)此测试方法,标注该程序是以JUnit的方式来运行的。
JUit3:
在方法前加上TestCase类的方法setUp(),在执行每一次测试方法之前都会被调用,可把在测试方法中都需要的程序放在这里;
最后加上tearDown()方法,在执行每一次方法后都会被调用。
JUit4:
与JUit3不同的是,在JUit4中是在方法前用注解
@BeforeClass:
globeInit(),无论执行多少测试方法,它只执行一次,且运行在最前
@Before:
把方法注解成Before,init()与JUit3中的setUp()方法作用一样
@After:
把方法注解成After,destroy()与JUit3中的tearDown()方法作用一样
@AfterClass:
无论执行多少测试方法,它只执行一次,且运行在最后
下面分别以JUit3和JUit4为例子来运行测试:
例1.JUit3
源代码:
packagecom.sinyee.unit;
publicclassArrayUnit{
/**
*传入一个数组,返回该数组的最大值
*@paramarray
*@return
*/
publicintgetMaxValue(int[]array)throwsException{
if(array==null){
thrownewNullPointerException("
空指针异常"
);
}
if(array.length==0){
thrownewArrayIndexOutOfBoundsException("
数组不能为空!
"
}
inttemp=array[0];
for(inti=1;
i<
array.length;
i++){
if(temp<
array[i]){
temp=array[i];
}
//取出该数组的最大值
returntemp;
}
Junit3测试代码
importjunit.framework.TestCase;
publicclassArrayTestextendsTestCase{
//设置类的成员变量,可以供所有的方法调用
privateArrayUnitaUnit;
*该setUp()方法为TestCase里面的方法
*作用:
在每次执行调用测试方法之前,会先调用该setUp方法
@Override
protectedvoidsetUp()throwsException{
//实例化数组工具对象
aUnit=newArrayUnit();
System.out.println("
setUp()"
*求数组最大值的测试用例1:
数组不为空
publicvoidtestGetMaxValue1(){
//定义一个array数组
int[]array={40,3,2,6,9,30,4};
try{
//调用求数组最大值方法
intactual=aUnit.getMaxValue(array);
//期望值为40
intexpected=40;
//断言actual==expect
assertEquals(expected,actual);
}catch(Exceptione){
e.printStackTrace();
fail();
*求数组最大值的测试用例2:
数组为空
publicvoidtestGetMaxValue2(){
int[]array={};
//实例化数组对象
ArrayUnitaUnit=newArrayUnit();
aUnit.getMaxValue(array);
//若上面的方法有抛出异常,则fail()方法将不会被调用
//断言该异常为ArrayIndexOutOfBoundsException该类型的异常,且消息为"
assertEquals(ArrayIndexOutOfBoundsException.class,e.getClass());
assertEquals("
e.getMessage());
*求数组最大值的测试用例3:
空指针异常
publicvoidtestGetMaxValue3(){
int[]array=null;
//断言该异常为NullPointerException该类型的异常,且消息为"
assertEquals(NullPointerException.class,e.getClass());
protectedvoidtearDown()throwsException{
tearDown()"
}
例2.JUnit4
测试代码:
importstaticorg.junit.Assert.*;
importorg.junit.Before;
importorg.junit.Test;
/**
*Junit4的测试类该类无需继承任何类,测试方法无需与test开头
publicclassArrayUnitTest{
*把方法注解为@Before,效果与JUnit3中的setUp()方法一样
@Before
publicvoidinit(){
aUnit=newArrayUnit();
*Junit4须加上@Test此测试方法,标注该程序是以Junit的方式来运行的
*测试数组不为空
@Test
publicvoidgetMaxValueTest(){
int[]array={3,6,5,7,4,8,12,0};
intexpected=12;
//断言expected==actual
//Assert.assertEquals(expected,actual);
//或者也可以直接通过静态导入该Assert类的所有方法importstaticorg.junit.Assert.*;
*测试数组长度为零时,断言会抛出异常
*@throwsException
@Test(expected=ArrayIndexOutOfBoundsException.class)
publicvoidgetMaxValueTest2()throwsException{
int[]array={};
aUnit.getMaxValue(array);
*测试数组为null时,断言会抛出空指针异常
@Test(expected=NullPointerException.class)
publicvoidgetMaxValueTest3()throwsException{
int[]array=null;
*当测试用例还未完成时,可用注解@Ignore来标记这测试用例还未完成
@Ignore
publicvoidgetMaxValueTest4(){
*把方法注解为@After,效果与JUnit3中的tearDown()方法一样
@After
publicvoiddestroy(){
3.术语
Errors程序出错
Failures断言失败
4.批量测试
例1.JUnit3
importjunit.framework.Test;
importjunit.framework.TestSuite;
publicclassAllTestextendsTestCase{
//若要实现批量测试的话,须加上一个方法
publicstaticTestsuite(){
TestSuitetSuite=newTestSuite();
//添加计算器测试类
tSuite.addTestSuite(CalculateTest.class);
//添加数组最大值测试类
tSuite.addTestSuite(ArrayTest.class);
//添加堆栈测试类
tSuite.addTestSuite(StackTest.class);
returntSuite;
importorg.junit.runner.RunWith;
importorg.junit.runners.Suite;
importorg.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({CalculateUtilTest.class,ArrayUnitTest.class,
StackUtilTest.class,StringUtilTest.class})
publicclassTestAll{}
二.自动生成
选择所要测试的类,右击->
New->
选择Java下的Junit项目中的JunitTestCase,根据自己所要测试的版本类型选择进行操作.
三.WEB测试
需在lib包下拷入几个jar包:
httpunit-1.7文件夹的lib目录下的httpunit.jar以及jars目录下的。
例子1.
packagecom.junit.action;
importjava.io.IOException;
importorg.xml.sax.SAXException;
importcom.meterware.httpunit.GetMethodWebRequest;
importcom.meterware.httpunit.PostMethodWebRequest;
importcom.meterware.httpunit.WebConversation;
importcom.meterware.httpunit.WebRequest;
importcom.meterware.httpunit.WebResponse;
publicclassUserLoginActionTest{
*测试用正确的网址登录,断言登录成功
publicvoidtestDoGetHttpServletRequestHttpServletResponse(){
//创建出模拟的浏览器对象
WebConversationwc=newWebConversation();
//创建出get请求
WebRequestwRequest=newGetMethodWebRequest("
http:
//localhost:
8088/JUnitUserManage/UserLogin.html"
//使用浏览器获取该请求的响应内容
wc.getResponse(wRequest);
}catch(IOExceptione){
}catch(SAXExceptione){
*测试用一个不存在的地址登录,断言抛出异常
*@throwsSAXException
*@throwsIOException
@Test(expected=Exception.class)
publicvoidtestDoGetHttpServletRequestHttpServletResponse2()throwsIOException,SAXException{
8088/JUnitUserManage/UserLogin2.html"
//使用浏览器获取该请求的响应内容
wc.getResponse(wRequest);
publicvoidtestDoPostHttpServletRequestHttpServletResponse(){
//创建Post请求
WebRequestwRequest=newPostMethodWebRequest("
//模拟出请求所需要的参数
wRequest.setParameter("
userName"
"
admin"
pwd"
//获取该请求的响应内容
WebResponsewResponse=wc.getResponse(wRequest);
//获取响应内容的实际地址
Stringactual=wResponse.getURL().toString();
//期望在用户名和密码正确的情况下,跳转到页面"
8088/JUnitUserManage/Success.jsp"
Stringexpected="
;
//断言
*测试失败页面的返回按钮的链接地址
*断言是"
UserLogin.html"
publicvoidtestLink(){
//创建出"
8088/JUnitUserManage/Failure.jsp"
该地址的get请求方式
//获取该请求的响应内容
//根据链接显示内容,获取该连接
WebLinkwLink=wResponse.getLinkWith("
返回"
//获取该连接对应的href地址
Stringactual=wLink.getURLString();
//获取所期待的链接地址