2015-02-07 496 views
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; 
    } 
} 

請你能幫助我解決問題

+0

請給一個簡短而完整的程序演示問題。它不會*看起來像你應該能夠在那裏得到一個'NullPointerException'。目前還不清楚你爲什麼要使用Mockito作爲'AccountingYear',請注意你 - 你想要嘲笑什麼?另外請注意,您的'isOldAccountingYear'方法的主體會更清晰地寫爲'return endingDate2.before(startDate);'(爲什麼在'endingDate2'結尾處有2個?) – 2015-02-07 10:34:20

+0

您可以發佈完整的測試代碼(即'@ Before'等),我們不知道'startingDate','endingDate'是什麼,以及'daoFactory' /'lastAccoutingYear'如何處理' – 2015-02-07 10:34:30

+0

這是AccountingYear類的整個代碼 – gasmyr 2015-02-07 11:11:02

回答

1

你的症狀描述不清,但似乎你說的NPE被拋出before致電isOldAccountingYear。這意味着startDate參數是null。 (如果endingDate2null,那麼NPE將由isOldAccountingYear本身拋出,而不是由before法)

我認爲你必須在調用的代碼錯誤isOldAccountingYear

if (isOldAccountingYear(startDate, endingDate)) { 

現有代碼已仔細檢查了endingDate參數,它不能是null。但是startDate是一個局部變量,其值來自lastAccoutingYear。它可能是null。我懷疑它是......並導致NPE。

我不明白這個代碼的真正意圖(你的變量命名是沒有幫助的!),但我懷疑的是,上述行應該是:

if (isOldAccountingYear(startingDate, endingDate)) {