简体   繁体   English

测试类中的 java.util.logging.Logger

[英]java.util.logging.Logger in tested class

I'm trying to run a test for my class which uses java.util.logging.Logger我正在尝试为使用 java.util.logging.Logger 的类运行测试

import java.util.logging.Logger;

public class TestLogging {
    final Logger logger = Logger.getLogger("Test");

    public void f1() {
        logger.entering(getClass().getName(), "f1");

        logger.info("f1");
        logger.fine("f1");
        logger.finer("f1");
        logger.finest("f1");

        logger.exiting(getClass().getName(), "f1");
    }
}

So I setup a test class to check if logger produces an output while the class is being tested所以我设置了一个测试类来检查记录器在测试类时是否产生输出

import org.junit.Before
import org.junit.Test
import java.util.logging.Level
import java.util.logging.Logger

class TestLogger {
    @Before
    fun setupLogger() {
        Logger.getLogger("Test").level = Level.FINEST
    }

    @Test
    fun test() {
        TestLogging().f1()
    }
}

But when I run it I only see the following output, as if the level is set to default.但是当我运行它时,我只看到以下输出,就好像级别设置为默认值一样。

TestLogger > test STANDARD_ERROR
    Sep 27, 2017 2:17:32 PM TestLogging f1
    INFO: f1

You have to hold a strong reference to your logger that is wider than method local scope.您必须持有比方法本地范围更广泛的对记录器强引用 If you don't do that your level setting can be lost due to garbage collection of the logger.如果您不这样做,您的级别设置可能会由于记录器的垃圾收集而丢失。

If you want to see the output in the console then you also have to adjust the level of the console handler.如果您想在控制台中看到输出,那么您还必须调整控制台处理程序的级别。

If you want to assert some output value then you need to install a filter or handler to look for a specific LogRecord .如果要断言某些输出值,则需要安装过滤器或处理程序来查找特定的LogRecord

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM