如何在单元测试中抑制ERROR日志记录

时间:2016-12-01 14:48:43

标签: java unit-testing logging testng

在TestNG单元测试中,我测试了一个异常被捕获并记录下来。

正在测试此代码

    try {
        workloadServiceImpl.actionStarted(resourceType);
    } catch (Exception e) {
        LOGGER.error("Error measuring workload during actionStarted, resourceType=" + resourceType + ": " + e.getMessage(), e);
    }

通过这个测试

@Test
public void testMeasureWorkflow_actionStartedExceptionCaught() throws Throwable {
    doThrow(new RuntimeException()).when(workloadService).actionStarted(any());
    tested.measureWorkloadForDatabase(joinPoint);
}

但是在单元测试期间,我不希望记录异常。如果他们看到它会吸引其他开发人员注意,然后他们会浪费时间,想知道为什么日志中存在异常。是否存在一种机制,如何在单元测试期间禁止记录此异常?我们使用logback,但logback-test.xml也被集成测试使用,在那种测试中我不想抑制异常日志记录,因此将其修复在logback-test.xml文件中并不是首选方法。

2 个答案:

答案 0 :(得分:0)

我不熟悉logback,但您可以在运行之前关闭testMeasureWorkflow_actionStartedExceptionCaught中的ERROR日志记录吗?

答案 1 :(得分:0)

您希望创建一个过滤器,用于断言记录事件的值,并且不允许请求通过。

它与How to verify that error was logged with unit tests中所做的概念相同,但您必须将其移植到logback。

相关问题