(100分)
编写一个DateUtil的类,里面有一个isLeapYear(int year) 的方法,判断输入年份是否是闰年。如果是闰年,返回true,其他返回false。闰年需要满足以下3个条件:
年份必须大于0,且小于等于10000。
年份不能整除100,且可以整除4。
年份可以整除100,且可以整除400。
请编写JUnit测试类DateUtilTest,采用以下用例,使用assertEquals来测试程序正确性。-100, 1000, 20000, 2020, 2019, 2000, 1900.
DateUtil类
package it.qijian.cn;
public class DateUtil{
public boolean isLeapYear(int year) {
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){ //平闰年判断算法
return true;
}
else{
return false;
}
}
}
测试类:
package it.qijian.cn;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class DateUtilTest {
@Test
public void test() {
assertEquals(false, new DateUtil().isLeapYear(1900));
assertEquals(false, new DateUtil().isLeapYear(-100));
assertEquals(false, new DateUtil().isLeapYear(1000));
assertEquals(true, new DateUtil().isLeapYear(20000));
assertEquals(true, new DateUtil().isLeapYear(2020));
assertEquals(false, new DateUtil().isLeapYear(2019));
assertEquals(true, new DateUtil().isLeapYear(2000));
}
}
pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
运行结果: