【问题标题】:Mockito 1.9.5 causing NullPointerExceptionMockito 1.9.5 导致 NullPointerException
【发布时间】:2013-02-11 19:57:02
【问题描述】:

我有一个StandardUncaughtExceptionHandler,它可以捕获以前没有被其他异常捕获的任何异常。在后台,我使用 Guava EventBus 进行错误处理。对于我的应用程序中抛出的每种类型的检查异常,我向总线注册了一个事件处理程序来处理该特定异常类型。如果总线发布了一个没有注册处理程序的异常,它会将该异常包装在DeadEvent 对象中,并将死事件重新发送回总线。这个StandardUncaughtExceptionHandler 已注册为侦听DeadEvents,保证我总是有办法检查未捕获的异常。

这里是主要来源:

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 的任何想法?我检查并重新检查,我相信我已经正确设置了我的模型。提前致谢。

【问题讨论】:

    标签: java unit-testing nullpointerexception mockito


    【解决方案1】:

    Mockito 不是这里的问题。

    我相信 NPE 在您的测试的以下行中报告:

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

    因为rte.getMessage() 返回null。不幸的是,由于单元测试中的try-catch 块,这个错误的真正来源对你来说是隐藏的。在 handleUncaughtExceptionLogsThrowableIfPresent() 中取消注释 try-catch 揭示了真正的问题:在以下行中抛出了 NPE:

    loggingService.error(logBuilder.toString());

    因为loggingService 从未在StandardUncaughtExceptionHandler 类中初始化。此字段应在您的测试方法中使用模拟或任何其他有效方式初始化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 2012-10-14
      • 2014-07-27
      • 1970-01-01
      • 2016-03-07
      • 2017-05-16
      相关资源
      最近更新 更多