簡體   English   中英

Mockito 1.9.5導致NullPointerException

[英]Mockito 1.9.5 causing NullPointerException

我有一個StandardUncaughtExceptionHandler ,它捕獲以前未被其他異常捕獲的任何異常。 在引擎蓋下,我正在使用Guava EventBus進行錯誤處理。 對於我的應用程序中拋出的每種類型的已檢查異常,我使用總線注冊一個事件處理程序來處理該特定異常類型。 如果總線發布了一個沒有注冊處理程序的異常,它會將該異常包裝在DeadEvent對象中,並將死事件重新發送回總線。 注冊此StandardUncaughtExceptionHandler以偵聽DeadEvent ,保證我始終可以檢查未捕獲的異常。

這是主要來源:

public class StandardUncaughtExceptionHandler implements UncaughtExceptionHandler {
    private LoggingService loggingService;

    // Getter and setter for logginService.

    @Override @Subscribe
    public void handleUncaughtException(DeadEvent deadEvent) {
        // Log it.
        StringBuilder logBuilder = new StringBuilder();

        if(deadEvent.getEvent() instanceof Throwable) {
            Throwable throwable = (Throwable)deadEvent.getEvent();

            logBuilder.append("An uncaught exception occurred: ");
            logBuilder.append(throwable.getMessage());
            logBuilder.append(" - Stack trace: ");
            logBuilder.append(throwable.getStackTrace());
        }
        else
            logBuilder.append("Something weird happened.");

        loggingService.error(logBuilder.toString());
    }
}

我測試它,檢查以確保當我們給它一個Throwable它構造正確的日志消息。

@Test
public void handleUncaughtExceptionLogsThrowableIfPresent() {
    // GIVEN
    StandardUncaughtExceptionHandler fixture =
        new StandardUncaughtExceptionHandler();
    LoggingService mockLoggingService = Mockito.mock(LoggingService.class);
    DeadEvent mockDeadEvent = Mockito.mock(DeadEvent.class);

    Mockito.doThrow(new RuntimeException("Logging-Throwable"))
        .when(mockLoggingService)
        .error(Mockito.contains("An uncaught exception occurred:"));
    Mockito.doThrow(new RuntimeException("Logging-Something-Else"))
        .when(mockLoggingService)
        .error(Mockito.contains("Something weird happened."));
    Mockito.doReturn(new Throwable()).when(mockDeadEvent).getEvent();

    try {
        // WHEN
        fixture.handleUncaughtException(mockDeadEvent);

        Assert.fail();
    } catch(RuntimeException rte) {
        // THEN
        Assert.assertTrue(rte.getMessage().contains("Logging-Throwable"));
    }
}

當我運行此測試時,我在JUnit控制台中收到以下錯誤:

java.lang.NullPointerException
    at com.myapp.StandardUncaughtExceptionHandlerTest.handleUncaughtExceptionLogsThrowableIfPresent(StandardUncaughtExceptionHandlerTest.java:63)
    ... rest of stack trace omitted for brevity, it's huge

關於為什么Mockito導致NPE的任何想法? 我已經檢查並重新檢查,我相信我已經正確設置了我的模擬。 提前致謝。

Mockito不是問題。

我相信NPE會在您測試的以下行中報告:

Assert.assertTrue(rte.getMessage().contains("Logging-Throwable"));

因為rte.getMessage()返回null 不幸的是,由於單元測試中的try-catch塊,這個錯誤的真正起源對你來說是隱藏的。 handleUncaughtExceptionLogsThrowableIfPresent()取消注釋try-catch顯示真正的問題:NPE將在以下行中拋出:

loggingService.error(logBuilder.toString());

因為loggingService永遠不會在StandardUncaughtExceptionHandler類中初始化。 應使用模擬或任何其他有效方式在測試方法中初始化此字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM