开发者

log4j - when to use PropertyConfigurator in tests?

开发者 https://www.devze.com 2022-12-22 15:44 出处:网络
I know you\'re supposed to only load log4j properties once, so what is standard practice when you\'re doing unit tests? Should I load it in every unit test file? Should I put it in jUnit\'s setUp() 开

I know you're supposed to only load log4j properties once, so what is standard practice when you're doing unit tests? Should I load it in every unit test file? Should I put it in jUnit's setUp() 开发者_StackOverflow中文版method?

Thanks


Why do you want to load log4j properties in unit tests at the first place? In the normal course of events, unit tests are not supposed to produce log messages. IMO you should simply switch off logging (also for the code being unit tested). The only required output of unit tests is the summary what JUnit (or your favourite unit test framework) produces for you automatically, anything else is just clutter. I add this to the class setup method in my unit test classes:

Logger.getRootLogger().setLevel(Level.OFF);

The only exception is when I am putting together a new unit test for an as yet untested class / module (I am working with legacy code, trying to cover it with unit tests gradually), or when analysing a test that fails. Then I let the log messages go to the standard output to get clues about what went wrong somewhere inside the code I called. But once the unit test (or the application code) is fixed, I switch logging off again.

0

精彩评论

暂无评论...
验证码 换一张
取 消