0
我有这样的方法来测试Date.getMillisOf NullPointerException异常
public static AccountingYear newInstance(Date startingDate, Date endingDate) {
if ((startingDate == null) || (endingDate == null)) {
throw new AccountingRuntimeException(
"the range specify is not correct");
}
AccountingDaoFactory daoFactory = (AccountingDaoFactory) UnitOfWork
.getCurrent().getDaoFactory();
AccountingYear lastAccoutingYear = daoFactory.getAccountingYearDao()
.getLastAccountingYear();
Date startDate = lastAccoutingYear.startingDate;
Date endDate = lastAccoutingYear.endingDate;
if (lastAccoutingYear != null
&& isDateRangesOverlap(startDate, endDate, startingDate,
endingDate)) {
throw new AccountingYearCollisionException();
}
if (endingDate.before(startingDate)) {
throw new EndingDateIsBeforeStartingDateException();
}
AccountingYear newAccountingYear = new AccountingYear(startingDate,
endingDate, true);
if (isOldAccountingYear(startDate, endingDate)) {
newAccountingYear.setSatus(AccountingYearState.OLD_AND_NOT_CLOSED);
}
newAccountingYear.save();
return newAccountingYear;
}
这是相应的测试
@Test
public void newAccountingYearTest() throws Exception {
AccountingYear accountingYear = Mockito.mock(AccountingYear.class);
Mockito.when(accountingYear.getAllPeriods()).thenCallRealMethod();
objectToTest = AccountingYear.newInstance(startingDate, endingDate);
Assert.assertNotNull(objectToTest);
Assert.assertEquals(2, objectToTest.getAllPeriods().size());
Assert.assertEquals(AccountingPeriodType.Opening, objectToTest
.getAllPeriods().get(0).getType());
Assert.assertEquals(AccountingPeriodType.Closing, objectToTest
.getAllPeriods().get(1).getType());
}
当我运行测试我有这样的例外:显示java.lang.NullPointerException,在Java的.util.Date.getMillisOf,Date.before()。 这是isOldAccountingYear代码,让异常
public static boolean isOldAccountingYear(Date startDate, Date endingDate2) {
if (endingDate2.before(startDate)) {
return true;
} else {
return false;
}
}
请你能帮助我解决问题
请给一个简短而完整的程序演示问题。它不会*看起来像你应该能够在那里得到一个'NullPointerException'。目前还不清楚你为什么要使用Mockito作为'AccountingYear',请注意你 - 你想要嘲笑什么?另外请注意,您的'isOldAccountingYear'方法的主体会更清晰地写为'return endingDate2.before(startDate);'(为什么在'endingDate2'结尾处有2个?) – 2015-02-07 10:34:20
您可以发布完整的测试代码(即'@ Before'等),我们不知道'startingDate','endingDate'是什么,以及'daoFactory' /'lastAccoutingYear'如何处理' – 2015-02-07 10:34:30
这是AccountingYear类的整个代码 – gasmyr 2015-02-07 11:11:02