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