jUnit + jMock 和 log4j

发布于 2024-12-28 16:03:32 字数 659 浏览 2 评论 0原文

我已经给了一个任务,在其他程序员创建的程序上执行 JUnit + JMock。大多数类都有这个静态字段记录器,即:

static Log logger = LogFactory.getLog(SomeClass.class.getName());

我通过在 setUp() 方法中实例化它来创建 SomeClass 的实例。当我运行 jUnit 类时,我收到此错误消息:

log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException:  (No such file or directory)

我尝试通过在 setUp() 内调用 DOMConfigurator.configure("log4j.xml"); 来执行手动 log4j 配置 方法,但我仍然收到上面相同的错误消息。

问题是:

  • 如何在调用其他使用 LogFactory.getLog 的类的类中运行我的单元测试 + 模拟
  • 我应该在我的设置方法中配置 log4j 以便模拟和单元测试运行没有异常?
  • 我该怎么办。

I've given a task to do a JUnit + JMock on a program created by other programmer. Most of the class has this static field logger, i.e.:

static Log logger = LogFactory.getLog(SomeClass.class.getName());

I am creating an instance of SomeClass by instantiating it inside my setUp() method. When I run my jUnit class I am getting this error message:

log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException:  (No such file or directory)

I tried to do the the manual log4j configuration by calling DOMConfigurator.configure("log4j.xml"); inside the setUp() method but I'm still getting the same error message above.

The question is:

  • How can I run my unit test + mocking in a class which calls other class that uses LogFactory.getLog
  • Should I configure log4j inside my setup method so that the mocking and unit test runs without an exception?
  • How should I do it.

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

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

发布评论

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

评论(2

只为一人 2025-01-04 16:03:32

您有两个选择...

在 Maven 项目中,第二个选项很简单,因为您只需在 test/resources 文件夹中创建 log4j.properties 即可。

You have two options...

In Maven projects, the second option is easy since you simply need to create a log4j.properties in your test/resources folder.

我为君王 2025-01-04 16:03:32

实际上,我遇到了两种类型的问题,我认为其中一种是 jUnit 中的 log4j 问题。实际上,您可以继续使用 jUnit + jMock,即使 log4j 未正确配置,它也会抛出异常,但不会停止 jUnit 执行。

我遇到的问题是:
1. FileNotFoundException 异常,消息为“setFile(null, true) 调用失败”
2. jMock意外调用错误

我通过放置VM参数 -Dlog4j.configuration=file:/C:/log4j/log4j.xml.我收到一个异常,因为它在编译目录(编译的 java 类的默认输出文件夹)内使用 log4j.xml。该 log4j 有此参数 。因此 log4j 正在寻找文件名为 ${error.file} (不存在)的日志文件。就像我上面所说的,我通过在上面放置一个 VM 参数来解决它。

经验教训:如果您在 log4j 执行中遇到 FileNotFoundException,请尝试将 -Dlog4j.debug 作为 VM 参数,以查看 log4j 在何处提取配置文件。

我解决了第 2 个问题通过完成我正在测试的方法内的所有预期对象。虽然 jMock 消息有点含糊。

经验教训:意外调用通常意味着您在测试方法中缺少正在单元测试的方法中使用的对象或模拟对象。这也意味着您尚未对其尝试调用的对象设置期望

Actually there are two types of problem that I have which I thought to be one, a log4j problem in jUnit. You can actually continue with the jUnit + jMock even if log4j is not configured properly it will throw an exception but won't stop jUnit execution.

The problem that I have are:
1. FileNotFoundException exception with message "setFile(null, true) call failed"
2. jMock unexpected invocation error

I solved number 1 (even though I could still continue unit testing even with log4j exception) by putting a VM argument -Dlog4j.configuration=file:/C:/log4j/log4j.xml. I was getting an exception because it was using the log4j.xml inside the compiled directory (default output folder for compiled java classes). That log4j has this parameter <param name="File" value="${error.file}"/>. So log4j is looking for a log file that has a file name ${error.file} (which doesn't exist). Like what I said above I solved it by putting a VM argument above.

Lesson learned: If you are getting FileNotFoundException in log4j execution try to put -Dlog4j.debug as a VM argument to see where log4j is pulling the config file.

I solve number 2 by completing all the expected object inside the method that I am testing. Though jMock message is kinda ambiguous.

Lesson learned: Unexpected invocation usually means you are missing an objects or mock-objects in your test-method that are being used in the method that you are unit testing. It also means you haven't set an expectations on the object that it is trying to invoke

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