Home > Back-end >  mvn test JUnit5 log4j: Why logs printed in @BeforeAll and @BeforeEach are not output to console?
mvn test JUnit5 log4j: Why logs printed in @BeforeAll and @BeforeEach are not output to console?

Time:01-05

I create a simple maven project to describe my problem

<?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

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

<?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?

-------------------------------------------------------
 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

CodePudding user response:

The methods BeforeAll and BeforeEach are not executed due to surefire plugin. You should use at least 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>
  • Related