简体   繁体   中英

log4j - when to use PropertyConfigurator in tests?

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() 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM