如何对记录器中的消息进行 JUnit 断言 [英] How to do a JUnit assert on a message in a logger

查看:21
本文介绍了如何对记录器中的消息进行 JUnit 断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些被测代码调用 Java 记录器来报告其状态.在 JUnit 测试代码中,我想验证此记录器中是否创建了正确的日志条目.大致如下:

I have some code-under-test that calls on a Java logger to report its status. In the JUnit test code, I would like to verify that the correct log entry was made in this logger. Something along the following lines:

methodUnderTest(bool x){
    if(x)
        logger.info("x happened")
}

@Test tester(){
    // perhaps setup a logger first.
    methodUnderTest(true);
    assertXXXXXX(loggedLevel(),Level.INFO);
}

我认为这可以使用经过特殊调整的记录器(或处理程序或格式化程序)来完成,但我更愿意重新使用已经存在的解决方案.(而且,说实话,我不清楚如何从记录器获取 logRecord,但假设这是可能的.)

I suppose that this could be done with a specially adapted logger (or handler, or formatter), but I would prefer to re-use a solution that already exists. (And, to be honest, it is not clear to me how to get at the logRecord from a logger, but suppose that that's possible.)

推荐答案

非常感谢这些(令人惊讶的)快速且有用的答案;他们让我找到了解决方案的正确方法.

Thanks a lot for these (surprisingly) quick and helpful answers; they put me on the right way for my solution.

代码库是我想使用的,使用 java.util.logging 作为它的记录器机制,我觉得在这些代码中不够自如,无法将其完全更改为 log4j 或记录器接口/外观.但是根据这些建议,我改造"了一个 j.u.l.handler 扩展,这很好用.

The codebase were I want to use this, uses java.util.logging as its logger mechanism, and I don't feel at home enough in those codes to completely change that to log4j or to logger interfaces/facades. But based on these suggestions, I 'hacked-up' a j.u.l.handler extension and that works as a treat.

下面是一个简短的总结.扩展 java.util.logging.Handler:

A short summary follows. Extend java.util.logging.Handler:

class LogHandler extends Handler
{
    Level lastLevel = Level.FINEST;

    public Level  checkLevel() {
        return lastLevel;
    }    

    public void publish(LogRecord record) {
        lastLevel = record.getLevel();
    }

    public void close(){}
    public void flush(){}
}

显然,您可以从 LogRecord 中存储您喜欢/想要/需要的数量,或者将它们全部压入堆栈直到溢出.

Obviously, you can store as much as you like/want/need from the LogRecord, or push them all into a stack until you get an overflow.

在准备 junit-test 时,您创建了一个 java.util.logging.Logger 并向其添加这样一个新的 LogHandler:

In the preparation for the junit-test, you create a java.util.logging.Logger and add such a new LogHandler to it:

@Test tester() {
    Logger logger = Logger.getLogger("my junit-test logger");
    LogHandler handler = new LogHandler();
    handler.setLevel(Level.ALL);
    logger.setUseParentHandlers(false);
    logger.addHandler(handler);
    logger.setLevel(Level.ALL);

setUseParentHandlers() 的调用是为了让正常的处理程序静音,这样(对于这个 junit 测试运行)不会发生不必要的日志记录.做任何你的被测代码需要使用这个记录器,运行测试和 assertEquality:

The call to setUseParentHandlers() is to silence the normal handlers, so that (for this junit-test run) no unnecessary logging happens. Do whatever your code-under-test needs to use this logger, run the test and assertEquality:

    libraryUnderTest.setLogger(logger);
    methodUnderTest(true);  // see original question.
    assertEquals("Log level as expected?", Level.INFO, handler.checkLevel() );
}

(当然,您会将大部分工作移到 @Before 方法中并进行各种其他改进,但这会使本演示文稿变得混乱.)

(Of course, you would move large part of this work into a @Before method and make assorted other improvements, but that would clutter this presentation.)

这篇关于如何对记录器中的消息进行 JUnit 断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆