spring JUnit 测试用例中的 Log4j
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8272930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log4j in JUnit Test case
提问by maks
I have a spring bean which has a logger member and I use that logger to log some actions.
Also I have written a test case which uses SpringJUnit4ClassRunner. I have configured Log4j with a properties file and in each test case I initialize the logger with these properties:
我有一个 spring bean,它有一个记录器成员,我使用该记录器来记录一些操作。
我还写了一个测试用例,它使用SpringJUnit4ClassRunner. 我已经使用属性文件配置了 Log4j,并且在每个测试用例中,我使用以下属性初始化记录器:
@BeforeClass
public static void init() {
PropertyConfigurator.configure("src/com/config/log4j.properties");
}
when I run a test it gives me a warning
当我运行测试时,它给了我一个警告
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
However, the logger in my bean writes messages into the specified location in log4j.properties, i.e. it works fine. Why does log4j gives me such warnings?
但是,我的 bean 中的记录器将消息写入 log4j.properties 中的指定位置,即它工作正常。为什么 log4j 会给我这样的警告?
采纳答案by Prasanna Talakanti
I think reason is this code in SpringJUnit4ClassRunner
我认为原因是 SpringJUnit4ClassRunner 中的这段代码
public SpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
super(clazz);
if (logger.isDebugEnabled()) {
logger.debug("SpringJUnit4ClassRunner constructor called with [" + clazz + "].");
}
this.testContextManager = createTestContextManager(clazz);
}
If you don't want see the warning regarding log4j, Just do as @DN suggested just add log4j.properties file to your application class path, In case if you use maven directory structure add log4j.properties file to (src/main/resources) that will eliminate the warning.
如果您不想看到有关 log4j 的警告,请按照@DN 建议的操作将 log4j.properties 文件添加到您的应用程序类路径,如果您使用 maven 目录结构,请将 log4j.properties 文件添加到 (src/main/resources ) 将消除警告。
回答by wbdarby
I wanted to keep my log4j config out of the default classpath to use different logging with unit tests & production. I worked around it by subclassing SpringJUnit4ClassRunner and using a static class initializer.
我想将我的 log4j 配置保留在默认类路径之外,以便在单元测试和生产中使用不同的日志记录。我通过继承 SpringJUnit4ClassRunner 并使用静态类初始值设定项来解决它。
// The new test runner
public class JUnit4ClassRunner extends SpringJUnit4ClassRunner {
static {
try {
Log4jConfigurer.initLogging( "classpath:test/log4jconfig.xml" );
}
catch( FileNotFoundException ex ) {
System.err.println( "Cannot Initialize log4j" );
}
}
public JUnit4ClassRunner( Class<?> clazz ) throws InitializationError {
super( clazz );
}
}
The Change to the Test:
测试的变化:
@RunWith(JUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration
@Transactional
public class LmpDaoImplTest extends AbstractTransactionalJUnit4SpringContextTests {
...
Now log4j is configured before the Spring test runner is invoked.
现在,在调用 Spring 测试运行器之前配置了 log4j。
回答by Dave Newton
Because the Runnerexecutes runs before Log4J is initialized.
因为Runner在 Log4J 初始化之前执行运行。

