【发布时间】: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