MavenSurefire插件未从org.junit.jupiter.api包记录日志

当我的单元测试日志记录中出现一些不一致的行为时:

当我的单元测试日志记录中出现一些不一致的行为时:

通过 IntelliJ 的Run接口运行测试

通过mvn test运行测试

我真的想在运行mvn test时看到以下日志语句,但是它们似乎不可见:

May 12, 2022 12:00:13 PM org.junit.jupiter.api.ClOrderer$Random <clinit>
CONFIG: ClOrderer.Random default seed: 155513463168987
May 12, 2022 12:00:13 PM org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter logSuccessMessage
CONFIG: Using default cl orderer 'org.junit.jupiter.api.ClOrderer$Random' set via the 'junit.jupiter.testcl.order.default' configuration parameter.
May 12, 2022 12:00:13 PM org.junit.jupiter.api.MethodOrderer$Random <clinit>
CONFIG: MethodOrderer.Random default seed: 155513483010989

在我的pom.xml里面:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <configuration>
        <systemPropertyVariables>
            <java.util.logging.config.file>
                ${project.build.testOutputDirectory}/logging.properties
            </java.util.logging.config.file>
        </systemPropertyVariables>
    </configuration>
</plugin>

src/test/resources/logging.properties的内容:

.level=CONFIG
.handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=CONFIG

当通过 IntelliJ 的Run接口运行测试时,我可以看到以下输出,所需的日志朝向底部:

/Users/ian.jones/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home/bin/java -ea -Djava.util.logging.config.file=/Users/ian.jones/Development/my-app/target/test-cles/logging.properties -Didea.test.cyclic.buffer.size=1048576 -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=61653:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -clpath [omitted for brevity] com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.myorg.OpenApiDocsGeneratorTest
May 12, 2022 12:00:13 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
CONFIG: Discovered TestEngines with IDs: [junit-jupiter (group ID: org.junit.jupiter, artifact ID: junit-jupiter-engine, version: 5.8.2, location: jar:file:/Users/ian.jones/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.8.2/junit-jupiter-engine-5.8.2.jar!/org/junit/jupiter/engine/JupiterTestEngine.cl)]
May 12, 2022 12:00:13 PM org.junit.platform.launcher.core.ServiceLoaderRegistry load
CONFIG: Loaded PostDiscoveryFilter instances: []
May 12, 2022 12:00:13 PM org.junit.platform.launcher.core.ServiceLoaderRegistry load
CONFIG: Loaded LauncherDiscoveryListener instances: []
May 12, 2022 12:00:13 PM org.junit.platform.launcher.core.ServiceLoaderRegistry load
CONFIG: Loaded TestExecutionListener instances: [org.junit.platform.launcher.listeners.UniqueIdTrackingListener@2781e022]
May 12, 2022 12:00:13 PM org.junit.platform.launcher.core.LauncherConfigurationParameters loadClpathResource
CONFIG: Loading JUnit Platform configuration parameters from clpath resource [file:/Users/ian.jones/Development/my-app/target/test-cles/junit-platform.properties].
May 12, 2022 12:00:13 PM org.junit.platform.launcher.core.ServiceLoaderRegistry load
CONFIG: Loaded LauncherSessionListener instances: []
May 12, 2022 12:00:13 PM org.junit.platform.launcher.core.LauncherConfigurationParameters loadClpathResource
CONFIG: Loading JUnit Platform configuration parameters from clpath resource [file:/Users/ian.jones/Development/my-app/target/test-cles/junit-platform.properties].
May 12, 2022 12:00:13 PM org.junit.jupiter.api.ClOrderer$Random <clinit>
CONFIG: ClOrderer.Random default seed: 155513463168987
May 12, 2022 12:00:13 PM org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter logSuccessMessage
CONFIG: Using default cl orderer 'org.junit.jupiter.api.ClOrderer$Random' set via the 'junit.jupiter.testcl.order.default' configuration parameter.
May 12, 2022 12:00:13 PM org.junit.jupiter.api.MethodOrderer$Random <clinit>
CONFIG: MethodOrderer.Random default seed: 155513483010989
May 12, 2022 12:00:13 PM org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter logSuccessMessage
CONFIG: Using default method orderer 'org.junit.jupiter.api.MethodOrderer$Random' set via the 'junit.jupiter.testmethod.order.default' configuration parameter.

但是,当运行mvn clean install时,对我来说很重要的日志不会出现:

[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ my-app ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[ERROR] May 12, 2022 12:10:06 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
[ERROR] CONFIG: Discovered TestEngines with IDs: [junit-jupiter (group ID: org.junit.jupiter, artifact ID: junit-jupiter-engine, version: 5.8.2, location: jar:file:/Users/ian.jones/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.8.2/junit-jupiter-engine-5.8.2.jar!/org/junit/jupiter/engine/JupiterTestEngine.cl)]
[ERROR] May 12, 2022 12:10:06 PM org.junit.platform.launcher.core.ServiceLoaderRegistry load
[ERROR] CONFIG: Loaded PostDiscoveryFilter instances: []
[ERROR] May 12, 2022 12:10:06 PM org.junit.platform.launcher.core.ServiceLoaderRegistry load
[ERROR] CONFIG: Loaded LauncherDiscoveryListener instances: []
[ERROR] May 12, 2022 12:10:06 PM org.junit.platform.launcher.core.ServiceLoaderRegistry load
[ERROR] CONFIG: Loaded TestExecutionListener instances: [org.junit.platform.launcher.listeners.UniqueIdTrackingListener@76c3e77a]
[ERROR] May 12, 2022 12:10:06 PM org.junit.platform.launcher.core.LauncherConfigurationParameters loadClpathResource
[ERROR] CONFIG: Loading JUnit Platform configuration parameters from clpath resource [file:/Users/ian.jones/Development/my-app/target/test-cles/junit-platform.properties].
[ERROR] May 12, 2022 12:10:06 PM org.junit.platform.launcher.core.ServiceLoaderRegistry load
[ERROR] CONFIG: Loaded LauncherSessionListener instances: []
[INFO] Running ...

我已经尝试将maven-surefire-plugin的版本更改为3.0.0-M43.0.0-M6,但在这两种情况下,这会导致所有CONFIG 日志从输出中消失。

我也尝试明确设置有问题的类的处理程序,例如:

org.junit.jupiter.api.MethodOrderer$Random.handlers=java.util.logging.ConsoleHandler

但是,当使用Run运行测试时,所有这些都会导致重复的输出日志行,并且在使用mvn test运行测试时不会更改输出。

显然,它的部分工作是在两种情况下读取logging.config(如果我删除这个文件,所有的 CONFIG 日志在运行时消失mvn test)-但我不知道为什么行为是不同的,或者如何解决这个问题。

编辑:最小pom.xml文件

这是一个最小的pom.xml演示问题(我使用 Maven 3.8.5)

<?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>surefire-problem-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M7</version>
                <configuration>
                    <systemPropertyVariables>
                        <java.util.logging.config.file>
                            ${project.build.testOutputDirectory}/logging.properties
                        </java.util.logging.config.file>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
2

介绍

让我们将以下版本视为当前版本:

Maven Surefire 插件3.0.0-M7

JUnit5.8.2.

根本原因分析

Analysis

Maven Surefire 插件使用它的JUnitPlatformProvider类。它可以在“测试”部分之前的输出中观察到:

<…>
[INFO] --- maven-surefire-plugin:3.0.0-M7:test (default-test) @ the-project ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
<…>

请参见JUnitPlatformProvider.setupJunitLogger()方法的实现:maven-surefire/JUnitPlatformProvider.java at surefire-3.0.0-M7 · apache/maven-surefire

private static void setupJunitLogger()
{
    Logger logger = Logger.getLogger( "org.junit" );
    if ( logger.getLevel() == null )
    {
        logger.setLevel( WARNING );
    }
}

请注意条件:

logger.getLevel() == null

请参阅Logger.getLevel()方法的文档:

publicLevelgetLevel ()

获取为此记录器指定的日志级别。结果可能为 null,这意味着此记录器的有效级别将从其父级继承。

退货:

此记录器的级别

请注意声明:

结果可能为 null,这意味着该记录器的有效级别将从其父级继承。

分析总结

这是(见“分析”小节)为什么:

Maven Surefire 插件不考虑父记录器配置。

它似乎不是用户友好的行为。
也许,值得将其报告为 Maven Surefire 插件问题。

有必要引入org.junit包的显式记录器配置。

解决方案

有必要引入org.junit包的显式记录器配置:

org.junit.level=CONFIG

跟进:检查提供的完整pom.xml文件

我已经使用了提供的完整的pom.xml文件,并带有以下先决条件。

我使用了相同的先决条件来测试解决方案。

前提条件

添加了测试类

添加了 3 个注释为@TestMethodOrder(MethodOrderer.Random.cl)的测试类,如下所示:

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(MethodOrderer.Random.cl)
public cl Application1Test {
    @Test
    public void shouldAnswerWithTrue() {
        Assertions.ertTrue(true);
    }
    @Test
    public void shouldAnswerWithTrue2() {
        Assertions.ertTrue(true);
    }
}

启用并行测试执行

src/test/resources/junit-platform.properties文件的内容:

junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent

添加了与 JUnit 相关的日志级别

src/test/resources/logging.properties的内容:

.level=CONFIG
.handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=CONFIG
org.junit.level=CONFIG

输出

$ ./mvnw clean test 2>&1 | grep -F 'MethodOrderer.Random'
CONFIG: MethodOrderer.Random default seed: 7225497639767

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(961)
Web安装项目中的虚拟目录
上一篇
如何快速连续安装IIS服务器和托管包 而不会遇到错误
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(84条)