如何 ignore/mock Slf4j 日志行?
How to ignore/mock Slf4j log lines?
我将 Spring 引导从 2.5.10 升级到 2.5.12,它在 logback https://github.com/spring-projects/spring-boot/releases/tag/v2.5.11
中为我带来了重大变化
我的一些单元测试(使用 Mockito)因 NullPointerException 而中断,我将模拟异常传递到主代码中的日志行。
例如,这是我在主代码中的日志行,并且 class 用 lombok 的 @Slf4j
注释
log.warn("Exception occurred while doing something", exception);
早些时候,这条日志行没有抛出任何错误。
我在这里的意图不是模拟记录器,而是通过任何可能的解决方法忽略这一行(即使我必须模拟它)。
Lombok 在编译时添加了以下内容(不确定模拟是否适用于此处):
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(HelloWorld.class);
我的一个解决方法是不使用模拟异常并传递一个实际的异常,但这会使单元测试失去乐趣。
堆栈跟踪:
java.lang.NullPointerException
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:99)
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:62)
at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:119)
at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:419)
at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:383)
at ch.qos.logback.classic.Logger.warn(Logger.java:692)
at com.example.r.e.d.s.d.t.HelloWorld.executeInternal(HelloWorld.java:74)
at com.example.r.e.d.s.d.t.HelloWorldTest.testLocksOnDomain(HelloWorldTest.java:94)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.mockito.internal.runners.DefaultInternalRunner.evaluate(DefaultInternalRunner.java:54)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access0(ParentRunner.java:66)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:99)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:105)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:40)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Process finished with exit code 255
此外,我的单元测试使用以前的库版本。有一个问题,在执行单元测试期间,它曾经在控制台中打印 WARN 和 ERROR 之类的日志(有时会造成很多混乱)。
导致错误的行(在您提到的版本中)如下所示(参见 https://github.com/qos-ch/logback/blob/v_1.2.11/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxy.java#L99):
if (throwableSuppressed.length > 0) {
值得注意的是,您的问题已得到修复,但我不知道现在或将来哪个版本的 Logback 可能有它。 https://github.com/qos-ch/logback/commit/c34645b320d2b31ccaf0de9bb079391904352a28 包含以下相关更改:
已替换
if (throwableSuppressed.length > 0) {
和
// while JDK's implementation of getSuppressed() will always return a non-null array,
// this might not be the case in mocked throwables. We are being extra defensive here.
if (OptionHelper.isNotEmtpy(throwableSuppressed)) {
我自己通常不太热衷于模拟异常。但是,如果这是您的上下文要求,那么将 getSuppressed() 模拟为 return 一个 non-null(可能为空就可以)数组可能会解决问题。
我将 Spring 引导从 2.5.10 升级到 2.5.12,它在 logback https://github.com/spring-projects/spring-boot/releases/tag/v2.5.11
中为我带来了重大变化我的一些单元测试(使用 Mockito)因 NullPointerException 而中断,我将模拟异常传递到主代码中的日志行。 例如,这是我在主代码中的日志行,并且 class 用 lombok 的 @Slf4j
注释log.warn("Exception occurred while doing something", exception);
早些时候,这条日志行没有抛出任何错误。
我在这里的意图不是模拟记录器,而是通过任何可能的解决方法忽略这一行(即使我必须模拟它)。
Lombok 在编译时添加了以下内容(不确定模拟是否适用于此处):
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(HelloWorld.class);
我的一个解决方法是不使用模拟异常并传递一个实际的异常,但这会使单元测试失去乐趣。
堆栈跟踪:
java.lang.NullPointerException
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:99)
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:62)
at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:119)
at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:419)
at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:383)
at ch.qos.logback.classic.Logger.warn(Logger.java:692)
at com.example.r.e.d.s.d.t.HelloWorld.executeInternal(HelloWorld.java:74)
at com.example.r.e.d.s.d.t.HelloWorldTest.testLocksOnDomain(HelloWorldTest.java:94)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.mockito.internal.runners.DefaultInternalRunner.evaluate(DefaultInternalRunner.java:54)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access0(ParentRunner.java:66)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:99)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:105)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:40)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Process finished with exit code 255
此外,我的单元测试使用以前的库版本。有一个问题,在执行单元测试期间,它曾经在控制台中打印 WARN 和 ERROR 之类的日志(有时会造成很多混乱)。
导致错误的行(在您提到的版本中)如下所示(参见 https://github.com/qos-ch/logback/blob/v_1.2.11/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxy.java#L99):
if (throwableSuppressed.length > 0) {
值得注意的是,您的问题已得到修复,但我不知道现在或将来哪个版本的 Logback 可能有它。 https://github.com/qos-ch/logback/commit/c34645b320d2b31ccaf0de9bb079391904352a28 包含以下相关更改: 已替换
if (throwableSuppressed.length > 0) {
和
// while JDK's implementation of getSuppressed() will always return a non-null array,
// this might not be the case in mocked throwables. We are being extra defensive here.
if (OptionHelper.isNotEmtpy(throwableSuppressed)) {
我自己通常不太热衷于模拟异常。但是,如果这是您的上下文要求,那么将 getSuppressed() 模拟为 return 一个 non-null(可能为空就可以)数组可能会解决问题。