JUnit测试框架的使用(含测试用例)_第1页
JUnit测试框架的使用(含测试用例)_第2页
JUnit测试框架的使用(含测试用例)_第3页
JUnit测试框架的使用(含测试用例)_第4页
JUnit测试框架的使用(含测试用例)_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、软件测试实验报告计算机2010-1 姓名:周杰 学号:3100717102JUnit测试框架的使用【实验目的】1、掌握Junit测试框架的使用2、掌握测试用例的编写【实验环境】操作系统:Windows 7 (x64)浏览器:IE 9开发环境:Eclipse IDE for Java DevelopersA:Junit使用方法示例1查看运行结果:B:Junit使用方法示例21)在工程中添加类:2)写单元测试代码:3)进一步完善测试用例:4)查看分析运行结果,修改错误代码:改进后的方法:wordFormat4DBpublic static String wordFormat4DB(String n

2、ame)if(name = null)return null;Pattern p = Ppile("A-Z");Matcher m = p.matcher(name);StringBuffer sb = new StringBuffer();while(m.find()if(m.start() != 0)m.appendReplacement(sb, ("_"+m.group().toLowerCase();return m.appendTail(sb).toString().toLowerCase();修改后测试用例运行成功:实验2【实验目的】1、掌握

3、Junit测试框架的使用2、掌握测试用例的编写【实验环境】操作系统:Windows 7 (x64)浏览器:IE 9开发环境:Eclipse IDE for Java Developers【程序清单】package zhoujie; public class Date public Date() public Date(int year, int month, int day) super(); if (this.isDayValid(year, month, day) && this.isMonthValid(month) && this.isYearValid

4、(year) this.year = year; this.month = month; this.day = day; else throw new IllegalArgumentException("Please check your input!"); private int year = -1; private int month = -1; private int day = -1; public boolean isDayValid(int year, int month, int day) if (month = 4 | month = 6 | month =

5、 9 | month = 11) && (day <= 30 && day >= 1) return true; if (month = 4 | month = 6 | month = 9 | month = 11) && (day > 30 | day < 1) return false; if (month = 1 | month = 3 | month = 5 | month = 7| month = 8 | month = 10 | month = 12) && (day <= 31 &

6、;& day >= 1) return true; if (month = 1 | month = 3 | month = 5 | month = 7| month = 8 | month = 10 | month = 12) && (day > 31 | day < 1) return false; if(month = 2 && DateUtil.isLeapYear(year) && (day >= 1 && day <= 29) return true; if(month = 2 &a

7、mp;& DateUtil.isLeapYear(year) && (day < 1 | day > 29) return false; if(month = 2 && !DateUtil.isLeapYear(year) && (day >= 1 && day <= 28) return true; if(month = 2 && !DateUtil.isLeapYear(year) && (day < 1 | day > 28) return fals

8、e; return false; public boolean isMonthValid(int month) return month >= 1 && month <= 12; public boolean isYearValid(int year) return year > 0; public int getYear() return year; public void setYear(int year) if (this.isYearValid(year) this.year = year; else throw new IllegalArgument

9、Exception("Please check your input!"); public int getMonth() return month; public void setMonth(int month) if (this.isMonthValid(month) this.month = month; else throw new IllegalArgumentException("Please check your input!"); public int getDay() return day; public void setDay(int

10、day) if (this.year = -1 | this.month = -1) throw new IllegalStateException("You should set the year and month before day!"); if (this.isDayValid(year, month, day) this.day = day; else throw new IllegalArgumentException("Please check your input!"); 类DateUtil package zhoujie; publi

11、c class DateUtil public DateUtil() public static boolean isLeapYear(int year) if (year % 4 = 0 && year % 100 != 0) return true; if (year % 100 = 0 && year % 400 != 0) return false; if (year % 100 = 0 && year % 400 = 0) return true; return false; public static int getDayofYear

12、(Date date) int sum = 0; if (isLeapYear(date.getYear() switch (date.getMonth() case 1: sum = 0; break; case 2: sum = 31; break; case 3: sum = 60; break; case 4: sum = 91; break; case 5: sum = 121; break; case 6: sum = 152; break; case 7: sum = 182; break; case 8: sum = 213; break; case 9: sum = 244;

13、 break; case 10: sum = 274; break; case 11: sum = 305; break; case 12: sum = 335; break; default: System.out.print("data error"); break; else switch (date.getMonth() case 1: sum = 0; break; case 2: sum = 31; break; case 3: sum = 59; break; case 4: sum = 90; break; case 5: sum = 120; break;

14、 case 6: sum = 151; break; case 7: sum = 181; break; case 8: sum = 212; break; case 9: sum = 243; break; case 10: sum = 273; break; case 11: sum = 304; break; case 12: sum = 334; break; default: System.out.print("data error"); break; sum = sum + date.getDay(); return sum; 【测试用例】package zho

15、ujie;import static org.junit.Assert.*;import org.junit.Test;public class DateUtilTest Testpublic void testWordFormat4DB() String target = "employeeInfo"String result = DateUtil.wordFormat4DB(target);assertEquals("employee_info", result);/测试 null 时的处理情况Test public void wordFormat4

16、DBNull()String target = null;String result = DateUtil.wordFormat4DB(target);assertNull(result);/测试空字符串的处理情况Test public void wordFormat4DBEmpty()String target = ""String result = DateUtil.wordFormat4DB(target);assertEquals("", result);/测试当首字母大写时的情况Test public void wordFormat4DBegin()String target = "EmployeeInfo"String result = DateUtil.wordFormat4DB(target);assertEquals("employee_info", result);/测试当尾字母为大写时的情况Test public void wordFormat4DBEnd()String target = "employeeInfoA"String result = DateUtil.wordF

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论