java—如何在这些单元测试中捕获通量的日志输出?

n3h0vuf2  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(214)

我正在检查这个项目中的示例代码https://github.com/balamaci/reactor-core-playground#simple-操作员
下面是源代码中的一个典型函数

@Test
public void delayElements() {
    CountDownLatch latch = new CountDownLatch(1);

    log.info("Starting");
    Flux.range(0, 5)
            .doOnNext(val -> log.info("Emitted {}", val))
            .delayElements(Duration.of(2, ChronoUnit.SECONDS))
            .subscribe(
                    tick -> log.info("Tick {}", tick),
                    (ex) -> log.info("Error emitted"),
                    () -> {
                        log.info("Completed");
                        latch.countDown();
                    });

    Helpers.wait(latch);
}

我希望能够看到日志输出 Error emitted .
但是如果我跑了 mvn test 我根本看不到任何标准输出。
我曾尝试在surefire插件中启用日志捕获,但也没有实现任何效果

+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <configuration>
+            <redirectTestOutputToFile>true</redirectTestOutputToFile>
+          </configuration>
+        </plugin>

资源中有一个属性文件

org.slf4j.simpleLogger.logFile=System.out
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS
org.slf4j.simpleLogger.showThreadName=true
org.slf4j.simpleLogger.showShortLogName=true

但产量仍然很高

$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.balamaci:reactor-playground >-------------------
[INFO] Building Reactor-Core Playground 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ reactor-playground ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/antkong/experiments/flux/reactor-core-playground/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ reactor-playground ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ reactor-playground ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ reactor-playground ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ reactor-playground ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.657 s
[INFO] Finished at: 2020-08-21T18:27:58+10:00
[INFO] ------------------------------------------------------------------------

在这种情况下,如何捕获日志输出?

3wabscal

3wabscal1#

似乎在类路径上没有任何记录器实现。尝试添加以下内容:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

创造 resources/logback.xml ```

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
 ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<root level="info" additivity="false">
    <appender-ref ref="STDOUT"/>
</root>
qyzbxkaa

qyzbxkaa2#

这里的问题似乎缺乏 Test 单词在类名中,所以它们根本不运行。命令,如 mvn test -Dtest=Part02SimpleOperators ,如果你提供一个名字,工作就很好。

相关问题