log4j - 何时在测试中使用 PropertyConfigurator?

发布于 2024-08-24 13:56:57 字数 107 浏览 1 评论 0原文

我知道您应该只加载一次 log4j 属性,那么当您进行单元测试时,标准做法是什么?我应该将它加载到每个单元测试文件中吗?我应该把它放在 jUnit 的 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() method?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2024-08-31 13:56:57

为什么首先要在单元测试中加载 log4j 属性?在正常情况下,单元测试不应生成日志消息。 IMO 你应该简单地关闭日志记录(也适用于正在单元测试的代码)。单元测试唯一需要的输出是 JUnit(或您最喜欢的单元测试框架)自动为您生成的摘要,其他任何东西都只是混乱。我将其添加到单元测试类中的类设置方法中:

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

唯一的例外是当我为尚未测试的类/模块组合新的单元测试时(我正在使用遗留代码,试图用单元测试覆盖它)逐渐),或者在分析失败的测试时。然后,我让日志消息发送到标准输出,以获取有关我调用的代码中出现问题的线索。但是,一旦单元测试(或应用程序代码)被修复,我就会再次关闭注销。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文