0

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

gasmyr
  • 11
  • 1
  • 4
  • Please show a short but complete program demonstrating the problem. It doesn't *look* like you should be able to get a `NullPointerException` there. It's not clear why you're using Mockito at all for `AccountingYear`, mind you - what are you trying to mock out? Also note that the body of your `isOldAccountingYear` method would be more clearly written as just `return endingDate2.before(startDate);` (And why do you have a 2 at the end of `endingDate2`?) – Jon Skeet Feb 07 '15 at 10:34
  • Can you post the full test code (i.e. `@Before` etc), we don't know what `startingDate`, `endingDate` are, and how is `daoFactory` / `lastAccoutingYear` "handled" –  Feb 07 '15 at 10:34
  • This is the whole code of tthe AccountingYear class – gasmyr Feb 07 '15 at 11:11
  • stupid me, by the headline I thought someone tries to get the millis of a NPE. – sschrass Feb 07 '15 at 11:50

1 Answers1

1

Your description of the symptoms is unclear, but it seems you are saying that an NPE is thrown within the before call in isOldAccountingYear. That implies that the startDate parameter is null. (If endingDate2 was null, then the NPE would be thrown by the isOldAccountingYear itself, not by the before method.)

I think you have a mistake in the code that calls isOldAccountingYear:

if (isOldAccountingYear(startDate, endingDate)) {

The prior code has carefully checked the endingDate parameter and it can't be null. But startDate is a local variable whose value comes from a field of lastAccoutingYear. It could be null. I suspect that it is ... and that leads to the NPE.

I don't understand the real intent of this code (your variable naming is not helpful!) but I suspect that the above line should be:

if (isOldAccountingYear(startingDate, endingDate)) {
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216