java - Date.getMillisOf NullPointerException

标签 java junit powermock

我有这个方法要测试

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;
    }
}

请你帮我解决这个问题

最佳答案

您对症状的描述不清楚,但您似乎是说在 isOldAccountingYear 中的 before 调用抛出了 NPE。这意味着 startDate 参数为 null。 (如果 endingDate2null,则 NPE 将由 isOldAccountingYear 本身抛出,而不是由 before 方法抛出.)

我认为您在调用 isOldAccountingYear 的代码中存在错误:

if (isOldAccountingYear(startDate, endingDate)) {

前面的代码已经仔细检查了endingDate参数,它不能为null。但 startDate 是一个局部变量,其值来自 lastAccoutingYear 字段。它可能是null。我怀疑是……这导致了 NPE。

我不明白这段代码的真正意图(你的变量命名没有帮助!)但我怀疑上面的行应该是:

if (isOldAccountingYear(startingDate, endingDate)) {

关于java - Date.getMillisOf NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28381056/

相关文章:

java - 我收到 FileNotFoundException

java - JNA/DLL 调用后 System.out.println() 停止工作?

java - fluffycat 示例中 Builder 加 Singleton 模式的困惑

java - 在参数化的junit测试用例中运行特定的测试用例

testing - Cucumber 和 JUnit 测试

java - 如何使用 Java 8 在与 ifPresent 相同的条件下返回 orElseThrow?

android - 为什么在运行 Android 本地单元测试时 Log.d() 什么也不打印?

junit - java.lang.ClassFormatError : Invalid method Code length while using PowerMock

java - @Mock 注释不适用于对象映射器

maven - Powermock 和 JMockit 单元测试的测试覆盖率