替代SLF4J绑定或配置单元测试? [英] Alternate SLF4J Binding or Config For Unit Test?
问题描述
我希望能够将用于单元测试的SLF4J的绑定切换到简单版本,但似乎没有简单的方法来配置它。基本上,我构建的项目记录到一个文件,我希望它在单元测试时成为控制台。
I'd like to be able to switch the bindings for SLF4J for unit testing over to the simple version, but it seems there is no easy way to config this. Basically, my built project logs to a file and I'd like it to be console instead when unit testing.
我之前使用过antrun maven插件用persistence.xml文件做了类似的事情,但这个解决方案看起来有点沉重。
I've used the antrun maven plugin before to do something like this with persistence.xml files, but that solution seems a bit heavy handed.
有没有人在单元测试中有使用备用配置或绑定的解决方案?
Does anyone out there have a solution to using alternate configs or bindings in unit tests?
TIA
推荐答案
更好的方法是促进您的日志记录框架。例如,Logback库首先搜索 logback-test.xml
,如果它不可用,它会查找 logback.xml
。如果在 / src / test / resources
中放置 logback-test.xml
,它将被选中用于单元测试。在此文件中,您配置控制台日志记录而不是文件。
A better approach is to facilitate your logging framework. Logback library for instance first searches for logback-test.xml
and if it is not available, it looks for logback.xml
. If you place logback-test.xml
in /src/test/resources
, it will be picked up for unit tests. In this file you configure console logging instead of file.
如果您仍在使用Log4J,只需放置 log4j.xml
在 / src / test / resources
中 - 在 / src / main / resources 之前,该文件夹在classpath 上可用code>,因此Log4J将使用它而不是普通的
/ src / main / resources
版本,同时仍然为最终版本加载后一版本( / src / test / resources
甚至不可用。)
If you are still using Log4J, simply place log4j.xml
in /src/test/resources
- this folder is available on classpath before /src/main/resources
, so Log4J will use it instead of ordinary /src/main/resources
version, while still loading the latter version for final builds (/src/test/resources
isn't even available then).
这篇关于替代SLF4J绑定或配置单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!