maven Junit 测试判断是否是闰年

(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>

运行结果:
在这里插入图片描述

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值