【问题标题】:Configure unit tests so that log4j configuration is not required配置单元测试,以便不需要 log4j 配置
【发布时间】:2018-07-15 15:39:24
【问题描述】:

我想用外部 log4j2 配置构建 spring-boot 应用程序。我创建了 log4j 配置与 spring-boot 应用程序 jar 文件捆绑在一起的项目,但它存储在 jar 文件之外。应用程序加载在 application.properties 中定义的配置文件,例如。 logging.config=file:config/log4j2.xml。应用结构如下所示:

app.jar 
config:
    ->application.properties
    ->log4j2.xml

应用程序正确启动并看到两个配置文件。但是测试无法找到 application.properties 中定义的 log4j2.xml。

我创建了 test.properties 文件如下:

logging.level.com.mypackage=TRACE

# This sets the global logging level and specifies the appenders
log4j.rootLogger=TRACE, theConsoleAppender

# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%d %p %c{10} [%t] %m%n

并用以下注释我的测试类:

@TestPropertySource(locations="classpath:test.properties")

但测试运行器仍会查找在 logging.config 中定义的文件。

我应该在 application.properties 文件中覆盖哪些属性,以便在单元测试期间不需要 log4j2.xml?

感谢您的帮助。

【问题讨论】:

    标签: unit-testing spring-boot log4j2


    【解决方案1】:

    我能想到的最佳想法是覆盖属性 logging.config 以指向类路径中包含的 log4j 配置,并将 log4j 配置添加为测试资源。

    在我的 test.properties 中

    logging.config=classpath:log4j2.xml
    

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      相关资源
      最近更新 更多