幕测平台幕测平台_使用使用Junit测试异常测试异常,测试测试NextDay函数函数
需求需求
NextDay是一个简单的日期计算器,计算给定日期的下一天的具体日期。如给定2020年1月1日,返回2020年1月2日;给定
2020年1月31日,返回2020年2月1日。主要要求考察学生对日期边界以及异常处理的测试的能力。
实现实现
NextdayTest.java
package net.test;
import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.containsString;
import net.mooctest.*;
import org.junit.Assert;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class NextdayTest {
// 有效
@Test
public void testDate01() {
Date date = new Date(2,29,2016);
Date d = Nextday.nextDay(date);
String result = d.toString();
Assert.assertEquals("3/1/2016",result);
}
// 抛出异常
@Test
public void testDate02(){
try{
Date date = new Date(-1,1,1901);
Date d = Nextday.nextDay(date);
}catch (IllegalArgumentException e){
assertThat(e.getMessage(), containsString("Not a valid month"));
}
}
// 以此类推
}
测试程序内容测试程序内容
Year.java
package net.mooctest;
public class Year extends CalendarUnit {
public Year(int pYear) {
setYear(pYear);
}
public void setYear(int pYear) {
setCurrentPos(pYear);
if (!this.isValid()) {
throw new IllegalArgumentException("Not a valid month");
}
}
public int getYear() {
return currentPos;
}
public boolean increment() {
currentPos = currentPos + 1;
if (currentPos == 0)
评论0