logo资料库

幕测平台_使用Junit测试异常,测试NextDay函数.pdf

第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
资料共5页,全文预览结束
幕测平台_使用使用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)
currentPos = 1; return true; } public boolean isLeap() { if (currentPos >= 0 && (((currentPos % 4 == 0) && (currentPos % 100 != 0)) || (currentPos % 400 == 0))) return true; else if (currentPos < 0 && ((((currentPos * -1) % 4 == 1) && ((currentPos * -1) % 100 != 1)) || ((currentPos * -1) % 400 == 1))) return true; return false; } protected boolean isValid() { if (this.currentPos != 0) return true; return false; } public boolean equals(Object o) { if (o instanceof Year) { if (this.currentPos == ((Year) o).currentPos) return true; } return false; } } Month.java package net.mooctest; public class Month extends CalendarUnit { private Year y; private int[] sizeIndex = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; public Month(int pMonth, Year y) { setMonth(pMonth, y); } public void setMonth(int pMonth, Year y) { setCurrentPos(pMonth); this.y = y; if (!this.isValid()) { throw new IllegalArgumentException("Not a valid month"); } } public int getMonth() { return currentPos; } public int getMonthSize() { if (y.isLeap()) sizeIndex[1] = 29; else sizeIndex[1] = 28; return sizeIndex[currentPos - 1]; } public boolean increment() { currentPos += 1; if (currentPos > 12) return false; else return true; } public boolean isValid() { if (y != null && y.isValid()) if (this.currentPos >= 1 && this.currentPos <= 12)
return true; return false; } public boolean equals(Object o) { if (o instanceof Month) { if (this.currentPos == ((Month) o).currentPos && this.y.equals(((Month) o).y)) return true; } return false; } } Day.java package net.mooctest; public class Day extends CalendarUnit { private Month m; public Day(int pDay, Month m) { setDay(pDay, m); } public boolean increment() { currentPos += 1; if (currentPos = 1 && this.currentPos <= m.getMonthSize()) return true; return false; } public boolean equals(Object o) { if (o instanceof Day) { if (this.currentPos == ((Day) o).currentPos && this.m.equals(((Day) o).m)) return true; } return false; } } Date.java package net.mooctest; public class Date { private Day d; private Month m; private Year y; public Date(int pMonth, int pDay, int pYear) { y = new Year(pYear); m = new Month(pMonth, y); d = new Day(pDay, m); } public void increment() { if (!d.increment()) { if (!m.increment()) { y.increment(); m.setMonth(1, y); } d.setDay(1, m); } } public void printDate() { System.out.println(m.getMonth() + "/" + d.getDay() + "/" + y.getYear()); } public Day getDay() {
return d; } public Month getMonth() { return m; } public Year getYear() { return y; } public boolean equals(Object o) { if (o instanceof Date) { if (this.y.equals(((Date) o).y) && this.m.equals(((Date) o).m) && this.d.equals(((Date) o).d)) return true; } return false; } public String toString() { return (m.getMonth() + "/" + d.getDay() + "/" + y.getYear()); } } Nextday.java package net.mooctest; public class Nextday { public static Date nextDay(Date d) { Date dd = new Date(d.getMonth().getCurrentPos(), d.getDay().getCurrentPos(), d.getYear().getCurrentPos()); dd.increment(); return dd; } } CalendarUnit.java package net.mooctest; public abstract class CalendarUnit { protected int currentPos; protected void setCurrentPos(int pCurrentPos) { currentPos = pCurrentPos; } protected int getCurrentPos() { return currentPos; } protected abstract boolean increment(); protected abstract boolean isValid(); } 南隅笙箫 原创文章 87获赞 8访问量 7118 关注 私信 展开阅读全文
作者:南隅笙箫
分享到:
收藏