题目要求
测试以下程序:该程序有三个输入变量month、day、year(month、day和year均为整数值,并且满足:1≤month≤12、1≤day≤31和1900≤year≤2050),分别作为输入日期的月份、日、年份,通过程序可以输出该输入日期在日历上隔一天的日期。例如,输入为2004年11月30日,则该程序的输出为2004年12月1日。
划分等价类,按照等价类划分法设计测试用例;
编写getNextDate函数;
掌握Junit4的用法,使用Junit4测试getNextDate函数。
等价类表
假设输入格式为year,month,day,且三个输入变量year、month和day均被输入。
year要区分闰年和平年,其中闰年还可以分为世纪闰年和普通闰年,且year要属于[1900,2050]。
month要根据该月有几天来进行区分,并且需要考虑是否向year进位,且month要属于[1,12]。
day要根据月份来判断天数是否合法,并且需要考虑是否向month进位,且day要属于[1,31]。
等价类划分如下。
![等价类划分表.jpg](https://images.cnblogs.com/cnblogs_com/chouxianyu/1511971/o_200427190527%E7%AD%89%E4%BB%B7%E7%B1%BB%E5%88%92%E5%88%86%E8%A1%A8.jpg)
测试用例
有效等价类测试用例
共有5个有效等价类测试用例。
测试数据
期望结果
覆盖范围
2004/12/25
2004/12/26
2,10,14
2001/2/28
2001/3/1
3,7,15
2000/2/29
2000/3/1
1,7,16
2001/4/30
2001/5/1
3,8,17
2001/5/31
2001/6/1
3,9,18
无效等价类测试用例
共有12个有效等价类测试用例。
测试数据
期望结果
覆盖范围
1899/6/1
year非法
4
2051/6/1
year非法
5
a/6/1
year非法
6
1999/0/1
month非法
11
1999/13/1
month非法
12
1999/a/1
month非法
13
1999/1/0
day非法
19
1999/1/32
day非法
20
1999/1/a
day非法
21
2001/2/29
day非法
22
2000/2/30
day非法
23
2001/4/31
day非法
24
源代码
项目结构如下图所示
![项目结构.jpg](https://images.cnblogs.com/cnblogs_com/chouxianyu/1511971/o_200427190552%E9%A1%B9%E7%9B%AE%E7%BB%93%E6%9E%84.jpg)
DateUtil.java
package com.company;
public class DateUtil {
// 有31天的月份
private static int[] monthOfThirtyOne = new int[]{1,3,5,7,8,10,12};
// 有30天的月份
private static int[] monthOfThirty = new int[]{4,6,9,11};
// 年月日
private int year;
private int month;
private int day;
// 最终实现的功能,输入是一个“年/月/日”格式的字符串;
// 如果函数运行成功,输出则是相同格式的下一天,否则是错误信息
public String getNextDate(String dateStr){
String updateResult = this.updateDate(dateStr);
// 如果输入合法
if (updateResult.equals("success")){
String checkResult = this.checkDate();
// 如果输入合法
if (checkResult.equals("valid")){
// 计算明天的日期
return this.calcNextDate();
}
return checkResult;
}
return updateResult;
}
// 根据输入字符串转换并更新年月日
private String updateDate(String dateStr){
// 获取年月日
String[] numbers = dateStr.split("/");
try{
this.year = Integer.parseInt(numbers[0]);
}catch (NumberFormatException e){
return "year非法";
}
try{
this.month = Integer.parseInt(numbers[1]);
}catch (NumberFormatException e){
return "month非法";
}
try{
this.day = Integer.parseInt(numbers[2]);
}catch (NumberFormatException e){
return "day非法";
}
return "success";
}
// 检查日期是否合法
private String checkDate(){
String valid = "valid";
String yearInvalid = "year非法";
String monthInvalid = "month非法";
String dayInvalid = "day非法";
// year合法
if (year>=1900&&year=1&&month |