【问题标题】:Extend Log4j2 Tests扩展 Log4j2 测试
【发布时间】:2021-03-18 11:56:07
【问题描述】:

我有一个 log4j2 Logger 类的包装器 - 例如

import org.apache.logging.log4j.core.Logger;
       
public class MyLogger extends Logger {
    protected MyLogger(LoggerContext context, String name, MessageFactory messageFactory) {
        super(context, name, messageFactory);
    }

    ....
    
}

我正在尝试扩展 LoggerTest 类以验证 MyLogger 功能(这样我就可以运行包含的测试并添加新的测试)。我尝试过类似以下的方法:

@SpringBootTest
@LoggerContextSource(
    value = "log4j-test2.xml",
    reconfigure = ReconfigurationPolicy.AFTER_EACH
)
public class MyLoggerTest extends LoggerTest {
    public MyLoggerTest(LoggerContext context, ListAppender app, ListAppender host, ListAppender noThrown) {
        super(context, app, host, noThrown);
    }
}

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.apache.logging.log4j.test.appender.ListAppender arg1] in constructor....

我尝试创建 @LoggerContextSource 的本地实现以及它引用的属性解析器/扩展类 (@Extensions({@ExtendWith({LoggerContextResolver.class}), @ExtendWith({ConfigurationResolver.class}), @ExtendWith({AppenderResolver.class})})),并改为引用它们 例如

@MyLoggerContextSource(
    value = "log4j-test2.xml",
    reconfigure = ReconfigurationPolicy.AFTER_EACH
)
...
@Extensions({
   @ExtendWith({MyLoggerContextResolver.class}), 
   @ExtendWith({MyConfigurationResolver.class}), 
   @ExtendWith({MyAppenderResolver.class})
})
public @interface MyLoggerContextSource {
...
}

LoggerContextResolverMyLoggerContextResolver 出现 Duplicate ParameterResolver... 错误

无论如何我可以扩展 log4j2 测试吗?

【问题讨论】:

  • 我绝不是 junit 专家,但我注意到您省略了参数上的 @Named 注释。虽然我没有编写 Log4j 单元测试工具,但我敢打赌,如果没有,构造函数的参数将为 null。
  • @rgoers 原来是@Named。我确信我之前尝试过它但它没有工作,但也许我当时也错过了其他东西(仍然是 java/spring 的新手......)。无论如何它现在正在工作,谢谢!

标签: java spring-boot logging log4j2 spring-boot-test


【解决方案1】:

感谢@rgoers,原来我错过了参数上的@Named 注释。

所以我的MyLoggerTest 适用于以下内容:

@SpringBootTest
@LoggerContextSource(
    value = "log4j-test2.xml",
    reconfigure = ReconfigurationPolicy.AFTER_EACH
)
public class MyLoggerTest extends LoggerTest {
    public MyLoggerTest(final LoggerContext context, @Named("List") final ListAppender app, @Named("HostTest") final ListAppender host, @Named("NoThrowable") final ListAppender noThrown) {
        super(context, app, host, noThrown);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 2011-02-21
    相关资源
    最近更新 更多