【发布时间】:2019-08-09 09:00:07
【问题描述】:
我使用JodaTime,我需要检查 2 个日期是否在 3 个月的差异范围内。所以我写了简单的方法来检查这个
private boolean inRangeOf3Months(Pair<BusinessData, BusinessData> pair) {
return pair.getKey().getDateTimeValue() != null && pair.getValue().getDateTimeValue() != null ?
new Period(pair.getKey().getDateTimeValue(), pair.getValue().getDateTimeValue()).getMonths() <= 3 : false;
}
现在我正在编写测试,这个很好
@Test
public void shouldReturnTrueWhenInRangeOf3Months() {
BusinessData closingDateFrom = businessData("closingDateFrom");
closingDateFrom.setDateTimeValue(DateTime.now());
BusinessData closingDateTo = businessData("closingDateTo");
closingDateTo.setDateTimeValue(DateTime.now().plusMonths(3));
ReportingSearchCriteria criteria = criteriaOf(closingDateFrom, closingDateTo);
Assert.assertTrue(validator.isSufficient(criteria));
}
但那个不是,我将第一个日期设置为now(),第二个日期设置为now().plusMonths(3).plusDays(1)。所以它超出了我的范围,不应该被允许。
@Test
public void shouldReturnFalseWhenOverRangeOf3Months() {
BusinessData closingDateFrom = businessData("closingDateFrom");
closingDateFrom.setDateTimeValue(DateTime.now());
BusinessData closingDateTo = businessData("closingDateTo");
closingDateTo.setDateTimeValue(DateTime.now().plusMonths(3).plusDays(1));
ReportingSearchCriteria criteria = criteriaOf(closingDateFrom, closingDateTo);
Assert.assertFalse(validator.isSufficient(criteria));
}
【问题讨论】: