如何在 gradle 启动测试中关闭关闭钩子的输出?

ams*_*ams 14 java gradle spring-boot

您可以从https://start.spring.io/starter.zip?type=gradle-project&language=java&bootVersion=2.2.5.RELEASE&baseDir=demo&groupId=com.example&artifactId=demo&name生成从 start.spring.io 到此问题的项目=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo&packaging=jar&javaVersion=1.8&dependencies=h2,data-jpa,web

我有一个用 gradle 构建的多模块 springBoot 应用程序,有一堆 SpringBoot 集成测试。当我进行构建时,我最终会得到一些从 SpringBoot 关闭到控制台的输出,如下所示。如何关闭此输出?

± |master ?1 {1} S:3 U:10 ?| ? ./gradlew build

> Task :core:test
2020-02-01 11:20:33.529  INFO 24114 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-02-01 11:20:33.531  INFO 24114 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-02-01 11:20:33.538  INFO 24114 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

> Task :email:test
2020-02-01 11:20:43.820  INFO 24150 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-02-01 11:20:43.820  INFO 24150 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-02-01 11:20:43.822  INFO 24150 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Shutdown initiated...
2020-02-01 11:20:43.822  INFO 24150 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-02-01 11:20:43.830  INFO 24150 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2020-02-01 11:20:43.830  INFO 24150 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Shutdown completed.

> Task :security:test
2020-02-01 11:20:54.941  INFO 24188 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-02-01 11:20:54.944  INFO 24188 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-02-01 11:20:54.952  INFO 24188 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.1.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 46s
57 actionable tasks: 54 executed, 3 up-to-date
Run Code Online (Sandbox Code Playgroud)

作为参考,从带有 gradle 的 start.spring.io 创建的应用程序在屏幕上不产生任何输出

./gradlew build

BUILD SUCCESSFUL in 779ms
5 actionable tasks: 5 up-to-date
Run Code Online (Sandbox Code Playgroud)

相反,输出被放置在 build/reports/

就我而言,我没有对 boot.log 附带的日志配置进行任何更改。没有 logback.xml,也没有更改 application.yml 的日志记录级别。我期待 gradle 正在捕获系统输出和系统错误并将它们发送到build/reports/但某些输出似乎正在逃逸到系统输出。

Mar*_*ler 3

人们可以禁用输出决定记录任务的内容,以控制测试 JVM 的 stdout/stderr:TestLoggingContainer testLogging.showStandardStreams = false onOutputTest

apply plugin: 'java'

test {

    // show standard out and standard error of the test JVM on the console
    // can be used to disable the console output:
    testLogging.showStandardStreams = true

    // listen to standard out and standard error of the test JVM
    // can be used to make the logging optional:
    onOutput { descriptor, event ->
        logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message)
    }
}
Run Code Online (Sandbox Code Playgroud)

这些流是TestLogEvent STANDARD_OUT& STANDARD_ERROR,它们来自 JVM。当可以确定event.message包含时extShutdownHook,可以跳过日志记录。