I have this method to test
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;
}
This is the corresponding test
@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());
}
When i run the test i have this exception: Java.lang.nullPointerexception, at Java.util.Date.getMillisOf,Date.before(). This is isOldAccountingYear code that give the exception
public static boolean isOldAccountingYear(Date startDate, Date endingDate2) {
if (endingDate2.before(startDate)) {
return true;
} else {
return false;
}
}
Please can you help me solve the problem