给你一个函数nextday,请分别用语句覆盖、判定覆盖、条件覆盖、判定条件覆盖、条件组合覆盖设计测试用例,并编写对应Junit代码进行测试
时间: 2023-09-07 12:17:43 浏览: 74
语句覆盖设计测试用例:
```
@Test
public void testNextDay() {
assertEquals("2022-01-02", nextday("2022-01-01"));
}
```
判定覆盖设计测试用例:
```
@Test
public void testNextDay() {
assertEquals("2022-01-02", nextday("2022-01-01"));
assertEquals("输入日期格式不正确", nextday("2022/01/01"));
}
```
条件覆盖设计测试用例:
```
@Test
public void testNextDay() {
assertEquals("2022-01-02", nextday("2022-01-01"));
assertEquals("输入日期格式不正确", nextday("2022/01/01"));
assertEquals("输入日期不存在", nextday("2022-02-31"));
}
```
判定条件覆盖设计测试用例:
```
@Test
public void testNextDay() {
assertEquals("2022-01-02", nextday("2022-01-01"));
assertEquals("输入日期格式不正确", nextday("2022/01/01"));
assertEquals("输入日期不存在", nextday("2022-02-31"));
assertEquals("输入日期不合法", nextday("2022-13-01"));
}
```
条件组合覆盖设计测试用例:
```
@Test
public void testNextDay() {
assertEquals("2022-01-02", nextday("2022-01-01"));
assertEquals("输入日期格式不正确", nextday("2022/01/01"));
assertEquals("输入日期不存在", nextday("2022-02-31"));
assertEquals("输入日期不合法", nextday("2022-13-01"));
assertEquals("2022-02-01", nextday("2022-01-31"));
}
```
对应Junit代码:
```
public String nextday(String date) {
String[] dates = date.split("-");
int year, month, day;
try {
year = Integer.parseInt(dates[0]);
month = Integer.parseInt(dates[1]);
day = Integer.parseInt(dates[2]);
} catch (NumberFormatException e) {
return "输入日期格式不正确";
}
if (month < 1 || month > 12) {
return "输入日期不合法";
}
if (day < 1 || day > 31) {
return "输入日期不合法";
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
return "输入日期不存在";
}
if (month == 2) {
if (day == 29 && !(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {
return "输入日期不存在";
}
if (day > 29) {
return "输入日期不存在";
}
}
day++;
if (day > getMonthMaxDay(year, month)) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
return String.format("%04d-%02d-%02d", year, month, day);
}
private int getMonthMaxDay(int year, int month) {
if (month == 2) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
return 31;
}
```
阅读全文