Log4j2:Junit测试用例失败

时间:2018-04-18 11:15:21

标签: java junit4 log4j2

我正在为log4j2 logger编写JUnit测试用例,如下所示

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SampleLoggerTest {
    private ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    private Logger logger = null;
    private PrintStream sysOut = System.out;

    @Before
    public void setUpStreams() {
        System.setOut(new PrintStream(outContent));
        logger = LogManager.getLogger(SampleLoggerTest.class);
    }

    @After
    public void restoreStreams() {
        System.setOut(sysOut);
    }

    @Test
    public void error() {
        logger.error("OnLogger");
        assertTrue(outContent.toString().contains("error"));
    }

    @Test
    public void debug() {
        logger.debug("OnLogger");
        assertTrue(outContent.toString().contains("debug"));
    }

    @Test
    public void fatal() {
        logger.fatal("OnLogger");
        assertTrue(outContent.toString().contains("fatal"));
    }

    @Test
    public void info() {
        logger.info("OnLogger");
        assertTrue(outContent.toString().contains("info"));
    }

    @Test
    public void warn() {
        logger.warn("OnLogger");
        assertTrue(outContent.toString().contains("warn"));
    }

    @Test
    public void trace() {
        logger.trace("OnLogger");
        assertTrue(outContent.toString().contains("trace"));
    }
}

但只有一个测试用例成功,即info() 我发现outContent包含第一个测试用例的日志语句,info()并且成功,但其余的测试用例outContent为空。

我尝试在restoreStreams()中刷新/关闭流但是没有工作,我还检查了控制台输出,所有内容都被打印出来,因为我将记录级别保持为trace

有人可以帮我找到我在这里缺少的东西吗?

以下是控制台输出:

2018-04-19 11:53:31.636 INFO [main] [com.logger.SampleLoggerTest] [info] [com.logger.SampleLoggerTest.info(SampleLoggerTest.java:60)] - logger
2018-04-19 11:54:39.197 WARN [main] [com.logger.SampleLoggerTest] [warn] [com.logger.SampleLoggerTest.warn(SampleLoggerTest.java:69)] - logger
2018-04-19 11:54:41.790 DEBUG [main] [com.logger.SampleLoggerTest] [debug] [com.logger.SampleLoggerTest.debug(SampleLoggerTest.java:42)] - logger
2018-04-19 11:54:43.966 ERROR [main] [com.logger.SampleLoggerTest] [error] [com.logger.SampleLoggerTest.error(SampleLoggerTest.java:33)] - logger
2018-04-19 11:54:46.254 FATAL [main] [com.logger.SampleLoggerTest] [fatal] [com.logger.SampleLoggerTest.fatal(SampleLoggerTest.java:51)] - logger
2018-04-19 11:54:48.309 TRACE [main] [com.logger.SampleLoggerTest] [trace] [com.logger.SampleLoggerTest.trace(SampleLoggerTest.java:78)] - logger

1 个答案:

答案 0 :(得分:0)

log4j仅初始化一次,因此@Before@After - 方法不会影响Log4J2。

SampleLoggerTest中的另一个问题是ByteArrayOutputStream,它被初始化一次,当SampleLoggerTest - 类的实例被创建并可能被关闭时,{ {1}} - 第二次调用方法。

以下代码适用于我:

@Before

执行上一个import java.io.ByteArrayOutputStream; import java.io.PrintStream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; public class SampleLoggerTest { private static ByteArrayOutputStream outContent; private static Logger logger = null; private static PrintStream printStream; @BeforeClass public static void init() { SampleLoggerTest.outContent = new ByteArrayOutputStream(); SampleLoggerTest.printStream = new PrintStream(SampleLoggerTest.outContent); System.setOut(SampleLoggerTest.printStream); SampleLoggerTest.logger = LogManager.getLogger(SampleLoggerTest.class); } @Test public void debug() { SampleLoggerTest.logger.debug("OnLogger"); Assert.assertTrue(SampleLoggerTest.outContent.toString().contains("DEBUG")); } @Test public void error() { SampleLoggerTest.logger.error("OnLogger"); Assert.assertTrue(SampleLoggerTest.outContent.toString().contains("ERROR")); } @Test public void fatal() { SampleLoggerTest.logger.fatal("OnLogger"); Assert.assertTrue(SampleLoggerTest.outContent.toString().contains("FATAL")); } @Test public void info() { SampleLoggerTest.logger.info("OnLogger"); Assert.assertTrue(SampleLoggerTest.outContent.toString().contains("INFO")); } @Test public void trace() { SampleLoggerTest.logger.trace("OnLogger"); Assert.assertTrue(SampleLoggerTest.outContent.toString().contains("TRACE")); } @Test public void warn() { SampleLoggerTest.logger.warn("OnLogger"); Assert.assertTrue(SampleLoggerTest.outContent.toString().contains("WARN")); } } - 方法后,在我的情况下为@Testtrace如下所示:

ByteArrayOutputStream