请编写对NextDay进行JUnit测试用的NextDay方法的代码
时间: 2024-02-12 21:06:05 浏览: 101
好的,以下是一个简单的对NextDay方法进行JUnit测试的代码示例:
```java
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class NextDayTest {
@Test
public void testNextDay() {
// Test case 1: normal case, next day is within the same month
Date date1 = new Date(2021, 10, 15);
Date expected1 = new Date(2021, 10, 16);
assertEquals(expected1, NextDay.nextDay(date1));
// Test case 2: normal case, next day is in the next month
Date date2 = new Date(2021, 12, 31);
Date expected2 = new Date(2022, 1, 1);
assertEquals(expected2, NextDay.nextDay(date2));
// Test case 3: edge case, leap year
Date date3 = new Date(2020, 2, 28);
Date expected3 = new Date(2020, 2, 29);
assertEquals(expected3, NextDay.nextDay(date3));
// Test case 4: edge case, leap year, next day is in the next month
Date date4 = new Date(2020, 2, 29);
Date expected4 = new Date(2020, 3, 1);
assertEquals(expected4, NextDay.nextDay(date4));
// Test case 5: edge case, end of the year
Date date5 = new Date(2021, 12, 31);
Date expected5 = new Date(2022, 1, 1);
assertEquals(expected5, NextDay.nextDay(date5));
}
}
```
其中,我们使用了JUnit的@Test注解来标记测试方法,使用了assertEquals方法来断言我们的预期结果与实际结果是否相等。我们编写了五个测试用例,分别测试了正常情况下的日期加一天,正常情况下的日期加一天跨月,闰年的2月28日加一天,闰年的2月29日加一天跨月,以及年底的最后一天加一天跨年。
阅读全文