Maven Surefire插件没有从org.junit.jupiter.api包中记录

u1ehiz5o  于 2022-11-11  发布在  Maven
关注(0)|答案(1)|浏览(251)

在以下情况下,我的单元测试日志记录中出现了一些不一致的行为:

  • 通过IntelliJ的Run接口运行测试
  • 通过mvn test运行测试

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

May 12, 2022 12:00:13 PM org.junit.jupiter.api.ClassOrderer$Random <clinit>
CONFIG: ClassOrderer.Random default seed: 155513463168987
May 12, 2022 12:00:13 PM org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter logSuccessMessage
CONFIG: Using default class orderer 'org.junit.jupiter.api.ClassOrderer$Random' set via the 'junit.jupiter.testclass.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-classes/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 -classpath [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.class)]
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 loadClasspathResource
CONFIG: Loading JUnit Platform configuration parameters from classpath resource [file:/Users/ian.jones/Development/my-app/target/test-classes/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 loadClasspathResource
CONFIG: Loading JUnit Platform configuration parameters from classpath resource [file:/Users/ian.jones/Development/my-app/target/test-classes/junit-platform.properties].
May 12, 2022 12:00:13 PM org.junit.jupiter.api.ClassOrderer$Random <clinit>
CONFIG: ClassOrderer.Random default seed: 155513463168987
May 12, 2022 12:00:13 PM org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter logSuccessMessage
CONFIG: Using default class orderer 'org.junit.jupiter.api.ClassOrderer$Random' set via the 'junit.jupiter.testclass.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.class)]
[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 loadClasspathResource
[ERROR] CONFIG: Loading JUnit Platform configuration parameters from classpath resource [file:/Users/ian.jones/Development/my-app/target/test-classes/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,这是部分工作(如果我删除此文件,则在运行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>
wmtdaxz3

wmtdaxz31#

简介

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

  • Maven Surefire插件3.0.0-M7.
  • JUnit 5.8.2的最新版本。

根本原因分析

分析
Maven Surefire插件使用了它的JUnitPlatformProvider类,可以在输出中的Tests部分之前看到:

<…>
[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()方法的实现:-JUnitPlatformProvider.java---------------------------------------

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

请注意以下情况:

logger.getLevel() == null

请参见Logger.getLevel()方法的文档:
公共Level获取级别()
获取为此事件记录程序指定的日志级别。结果可能为空值,这意味着此事件记录程序的有效级别将从其父级继承。
退货:
此记录器的级别
请注意以下声明:
结果可能为null,这意味着此记录器的有效级别将从其父记录器继承。

分析总结

这就是为什么(参见"分析“小节):

  1. Maven Surefire插件不考虑父记录器配置。
    这似乎不是一个用户友好的行为。
    也许,这是值得报告它作为一个Maven Surefire插件的问题。
    1.有必要为org.junit包引入显式记录器配置。

溶液

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

org.junit.level=CONFIG

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

我使用了提供的完整pom.xml文件,前提条件如下。
我用同样的前提条件来测试解决方案。

前提条件

添加的测试类

添加了3个使用@TestMethodOrder(MethodOrderer.Random.class)注解的测试类,如下所示:

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.class)
public class Application1Test {
    @Test
    public void shouldAnswerWithTrue() {
        Assertions.assertTrue(true);
    }

    @Test
    public void shouldAnswerWithTrue2() {
        Assertions.assertTrue(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

相关问题