实验三 单元测试
实验类别:基本实验
实验目的:
(1)掌握单元测试技术,并要求按照单元测试的要求设计测试用例。
(2)掌握在Eclipse里进行Junit4测试的技术。
实验内容:
日期问题
测试以下程序:该程序有三个输入变量month、day、year(month、day和year均为整数值,并且满足:1≤month≤12、1≤day≤31和1900≤ year ≤2050),分别作为输入日期的月份、日、年份,通过程序可以输出该输入日期在日历上隔一天的日期。例如,输入为 2004 年11月30日,则该程序的输出为2004年12月1日。
(1)边界值分析法设计测试用例;
(2)判定表驱动法设计测试用例。
实验要求:
(1)根据题目要求编写测试用例。
(2)实验结果要求给出测试用例集测试效果比较。
(3)撰写实验报告。
测试用例设计
边界值分析法设计测试用例
判定表驱动法设计测试用例
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String args[]) throws IOException {
int year, month, day;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("输入年:");
year = Integer.parseInt(br.readLine());
System.out.println("输入月:");
month = Integer.parseInt(br.readLine());
System.out.println("输入日:");
day = Integer.parseInt(br.readLine());
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
if (day == 31) {
day = 1;
month = month + 1;
} else
day = day + 1;
break;
case 4:
case 6:
case 9:
case 11:
if (day == 30) {
day = 1;
month = month + 1;
} else if(day==31)
{System.out.println("无效输入日期!");
continue;
}
else
day = day + 1;
break;
case 12:
if (day == 31) {
day = 1;
month = 1;
year = year + 1;
} else if (day < 31) {
day = day + 1;
}
break;
case 2: {
if (day == 28)
if (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
day = 29;
} else {
day = 1;
month = 3;
}
else if (day == 29) {
day = 1;
month = 3;
} else if (day < 28) {
day = day + 1;
} else{
System.out.println("无效输入日期!");
continue;
}
}
break;
default:
}
if (year >= 1900&& year <= 2050 && month <= 12 && month >= 1
&& day <= 31 && day >= 1) {
System.out.println("下一天是" + year + "年" + month + "月" + day
+ "日!");
} else if (year < 1900 || year > 2050) {
System.out.println("年的值不在指定范围内");
} else if (month > 12 || month < 1) {
System.out.println("月的值不在指定范围内");
} else if (day > 31 || day < 1) {
System.out.println("日的值不在指定范围内");
}
}
}
}
测试结果截图
错误结果1
错误结果2