JUnit测试框架的使用含测试用例.docx
《JUnit测试框架的使用含测试用例.docx》由会员分享,可在线阅读,更多相关《JUnit测试框架的使用含测试用例.docx(18页珍藏版)》请在冰豆网上搜索。
JUnit测试框架的使用含测试用例
[软件测试实验报告]计算机2010-1姓名:
周杰学号:
3100717102
JUnit测试框架的使用
【实验目的】
1、掌握Junit测试框架的使用
2、掌握测试用例的编写
【实验环境】
操作系统:
Windows7(x64)
浏览器:
IE9
开发环境:
EclipseIDEforJavaDevelopers
A:
Junit使用方法示例1
查看运行结果:
B:
Junit使用方法示例2
1)在工程中添加类:
2)写单元测试代码:
3)进一步完善测试用例:
4)查看分析运行结果,修改错误代码:
改进后的方法:
wordFormat4DB
publicstaticStringwordFormat4DB(Stringname){
if(name==null){
returnnull;
}
Patternp=Ppile("[A-Z]");
Matcherm=p.matcher(name);
StringBuffersb=newStringBuffer();
while(m.find()){
if(m.start()!
=0)
m.appendReplacement(sb,("_"+m.group()).toLowerCase());
}
returnm.appendTail(sb).toString().toLowerCase();
}
修改后测试用例运行成功:
实验2
【实验目的】
1、掌握Junit测试框架的使用
2、掌握测试用例的编写
【实验环境】
操作系统:
Windows7(x64)
浏览器:
IE9
开发环境:
EclipseIDEforJavaDevelopers
【程序清单】
packagezhoujie;
publicclassDate{
publicDate(){
}
publicDate(intyear,intmonth,intday){
super();
if(this.isDayValid(year,month,day)&&this.isMonthValid(month)
&&this.isYearValid(year)){
this.year=year;
this.month=month;
this.day=day;
}else{
thrownewIllegalArgumentException("Pleasecheckyourinput!
");
}
}
privateintyear=-1;
privateintmonth=-1;
privateintday=-1;
publicbooleanisDayValid(intyear,intmonth,intday){
if((month==4||month==6||month==9||month==11)&&(day<=30&&day>=1))
returntrue;
if((month==4||month==6||month==9||month==11)&&(day>30||day<1))
returnfalse;
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day<=31&&day>=1))
returntrue;
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day>31||day<1))
returnfalse;
if(month==2&&DateUtil.isLeapYear(year)&&(day>=1&&day<=29))returntrue;
if(month==2&&DateUtil.isLeapYear(year)&&(day<1||day>29))returnfalse;
if(month==2&&!
DateUtil.isLeapYear(year)&&(day>=1&&day<=28))returntrue;
if(month==2&&!
DateUtil.isLeapYear(year)&&(day<1||day>28))returnfalse;
returnfalse;
}
publicbooleanisMonthValid(intmonth){
returnmonth>=1&&month<=12;
}
publicbooleanisYearValid(intyear){
returnyear>0;
}
publicintgetYear(){
returnyear;
}
publicvoidsetYear(intyear){
if(this.isYearValid(year)){
this.year=year;
}else{
thrownewIllegalArgumentException("Pleasecheckyourinput!
");
}
}
publicintgetMonth(){
returnmonth;
}
publicvoidsetMonth(intmonth){
if(this.isMonthValid(month)){
this.month=month;
}else{
thrownewIllegalArgumentException("Pleasecheckyourinput!
");
}
}
publicintgetDay(){
returnday;
}
publicvoidsetDay(intday){
if(this.year==-1||this.month==-1)
thrownewIllegalStateException("Youshouldsettheyearandmonthbeforeday!
");
if(this.isDayValid(year,month,day)){
this.day=day;
}else{
thrownewIllegalArgumentException("Pleasecheckyourinput!
");
}
}
}
类DateUtil
packagezhoujie;
publicclassDateUtil{
publicDateUtil(){
}
publicstaticbooleanisLeapYear(intyear){
if(year%4==0&&year%100!
=0)
returntrue;
if(year%100==0&&year%400!
=0)
returnfalse;
if(year%100==0&&year%400==0)
returntrue;
returnfalse;
}
publicstaticintgetDayofYear(Datedate){
intsum=0;
if(isLeapYear(date.getYear())){
switch(date.getMonth()){
case1:
sum=0;break;
case2:
sum=31;break;
case3:
sum=60;break;
case4:
sum=91;break;
case5:
sum=121;break;
case6:
sum=152;break;
case7:
sum=182;break;
case8:
sum=213;break;
case9:
sum=244;break;
case10:
sum=274;break;
case11:
sum=305;break;
case12:
sum=335;break;
default:
System.out.print("dataerror");break;
}
}else{
switch(date.getMonth()){
case1:
sum=0;break;
case2:
sum=31;break;
case3:
sum=59;break;
case4:
sum=90;break;
case5:
sum=120;break;
case6:
sum=151;break;
case7:
sum=181;break;
case8:
sum=212;break;
case9:
sum=243;break;
case10:
sum=273;break;
case11:
sum=304;break;
case12:
sum=334;break;
default:
System.out.print("dataerror");break;
}
}
sum=sum+date.getDay();
returnsum;
}
}
【测试用例】
packagezhoujie;
importstaticorg.junit.Assert.*;
importorg.junit.Test;
publicclassDateUtilTest{
@Test
publicvoidtestWordFormat4DB(){
Stringtarget="employeeInfo";
Stringresult=DateUtil.wordFormat4DB(target);
assertEquals("employee_info",result);
}
//测试null时的处理情况
@TestpublicvoidwordFormat4DBNull(){
Stringtarget=null;
Stringresult=DateUtil.wordFormat4DB(target);
assertNull(result);
}
//测试空字符串的处理情况
@TestpublicvoidwordFormat4DBEmpty(){
Stringtarget="";
Stringresult=DateUtil.wordFormat4DB(target);
assertEquals("",result);
}
//测试当首字母大写时的情况
@TestpublicvoidwordFormat4DBegin(){
Stringtarget="EmployeeInfo";
Stringresult=DateUtil.wordFormat4DB(target);
assertEquals("employee_info",result);
}
//测试当尾字母为大写时的情况
@TestpublicvoidwordFormat4DBEnd(){
Stringtarget="employeeInfoA";
Stringresult=DateUtil.wordFormat4DB(target);
assertEquals("employee_info_a",result);
}
//测试多个相连字母大写时的情况
@TestpublicvoidwordFormat4DBTogether(){
Stringtarget="employeeAInfo";
Stringresult=DateUtil.wordFormat4DB(target);
assertEquals("employee_a_info",result);
}
}
【测试结果】