简体   繁体   English

mvn test + JUnit5 + log4j:为什么在@BeforeAll 和@BeforeEach 中打印的日志不是 output 到控制台?

[英]mvn test + JUnit5 + log4j: Why logs printed in @BeforeAll and @BeforeEach are not output to console?

I create a simple maven project to describe my problem我创建了一个简单的 maven 项目来描述我的问题

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>NoLog</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.9.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.19.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.19.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-jcl</artifactId>
      <version>2.19.0</version>
    </dependency>
  </dependencies>
</project>

my test class code我的测试 class 代码

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestDemo {

  static Logger log = LogManager.getLogger(TestDemo.class);
  @BeforeAll
  public static void setupClass() {
    log.info("BeforeAll 111 from log.info");
    System.out.println("BeforeAll 111 from print");
    log.info("BeforeAll 222 from log.info");
    System.out.println("BeforeAll 222 from print");
  }

  @BeforeEach
  public void setup() {
    log.info("BeforeEach 111 from log.info");
    System.out.println("BeforeEach 111 from print");
    log.info("BeforeEach 222 from log.info");
    System.out.println("BeforeEach 222 from print");
  }

  @Test
  public void test1() {
    log.info("test1 111 from log.info");
    System.out.println("test 111 from print");
    log.info("test1 222 from log.info");
    System.out.println("test 222 from print");
  }
}

and the log4j2.xml和 log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="LogToConsole" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="LogToConsole"/>
    </Root>
    <Logger name="com.hexagon" level="debug" additivity="false">
      <AppenderRef ref="LogToConsole"/>
    </Logger>
  </Loggers>
</Configuration>

My question is, when I run it using mvn test , I can only see the logs from @Test function, while I don't see the logs from @BeforeAll and @BeforeEach, why and how to enable it?我的问题是,当我使用mvn test运行它时,我只能看到来自@Test function 的日志,而看不到来自@BeforeAll 和@BeforeEach 的日志,为什么以及如何启用它?

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestDemo
2023-01-05 09:28:03 [main] INFO  TestDemo:29 - test1 111 from log.info
test 111 from print
2023-01-05 09:28:03 [main] INFO  TestDemo:31 - test1 222 from log.info
test 222 from print
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

The methods BeforeAll and BeforeEach are not executed due to surefire plugin.由于 surefire 插件,方法 BeforeAll 和 BeforeEach 没有被执行。 You should use at least maven-surefire-plugin#3.0.0-M1.您应该至少使用 maven-surefire-plugin#3.0.0-M1。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-report-plugin</artifactId>
  <version>3.0.0-M1</version>
</plugin>

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

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