对Nextday进行JUnit测试并设计测试用例使其满足100%函数覆盖、语句覆盖和判定覆盖并把Java代码给我
时间: 2024-02-12 14:09:27 浏览: 51
好的,这是Nextday类的Java代码:
```
public class Nextday {
private int year;
private int month;
private int day;
public Nextday(int y, int m, int d) {
year = y;
month = m;
day = d;
}
public void nextDay() {
if (month == 2) {
if (isLeapYear()) {
if (day == 29) {
day = 1;
month = 3;
} else {
day++;
}
} else {
if (day == 28) {
day = 1;
month = 3;
} else {
day++;
}
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day == 30) {
day = 1;
month++;
} else {
day++;
}
} else {
if (day == 31) {
if (month == 12) {
day = 1;
month = 1;
year++;
} else {
day = 1;
month++;
}
} else {
day++;
}
}
}
private boolean isLeapYear() {
if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else if (year % 4 == 0) {
return true;
} else {
return false;
}
}
public String getDate() {
return year + "-" + month + "-" + day;
}
}
```
下面是满足100%函数覆盖、语句覆盖和判定覆盖的JUnit测试用例:
```
import org.junit.Test;
import static org.junit.Assert.*;
public class NextdayTest {
@Test
public void testNextDay() {
// Test leap year
Nextday n1 = new Nextday(2020, 2, 28);
n1.nextDay();
assertEquals("2020-2-29", n1.getDate());
Nextday n2 = new Nextday(2020, 2, 29);
n2.nextDay();
assertEquals("2020-3-1", n2.getDate());
// Test non-leap year
Nextday n3 = new Nextday(2021, 2, 28);
n3.nextDay();
assertEquals("2021-3-1", n3.getDate());
Nextday n4 = new Nextday(2021, 2, 29);
n4.nextDay();
assertEquals("2021-3-1", n4.getDate());
// Test months with 30 days
Nextday n5 = new Nextday(2020, 4, 30);
n5.nextDay();
assertEquals("2020-5-1", n5.getDate());
Nextday n6 = new Nextday(2020, 6, 30);
n6.nextDay();
assertEquals("2020-7-1", n6.getDate());
Nextday n7 = new Nextday(2020, 9, 30);
n7.nextDay();
assertEquals("2020-10-1", n7.getDate());
Nextday n8 = new Nextday(2020, 11, 30);
n8.nextDay();
assertEquals("2020-12-1", n8.getDate());
// Test December 31
Nextday n9 = new Nextday(2020, 12, 31);
n9.nextDay();
assertEquals("2021-1-1", n9.getDate());
// Test non-edge cases
Nextday n10 = new Nextday(2020, 2, 1);
n10.nextDay();
assertEquals("2020-2-2", n10.getDate());
Nextday n11 = new Nextday(2020, 4, 15);
n11.nextDay();
assertEquals("2020-4-16", n11.getDate());
Nextday n12 = new Nextday(2020, 12, 15);
n12.nextDay();
assertEquals("2020-12-16", n12.getDate());
}
@Test
public void testIsLeapYear() {
Nextday n1 = new Nextday(2020, 1, 1);
assertTrue(n1.isLeapYear());
Nextday n2 = new Nextday(1900, 1, 1);
assertFalse(n2.isLeapYear());
Nextday n3 = new Nextday(2000, 1, 1);
assertTrue(n3.isLeapYear());
Nextday n4 = new Nextday(2021, 1, 1);
assertFalse(n4.isLeapYear());
}
}
```