在 Eclipse 中,当 Assert.assertEquals 失败时,如何查看它的输入?

发布于 2024-08-18 19:51:16 字数 541 浏览 11 评论 0原文

我不是 Eclipse 专家,所以请原谅我的笨拙。

在 Eclipse 中,当我调用 Assert.assertEquals(obj1,obj2) 但失败时,如何让 IDE 显示 obj1 和 obj2?

我正在使用 JExample,但我想这应该没有什么区别。


编辑:这是我所看到的:

(来源:yfrog.com

I'm not much of an Eclipse guru, so please forgive my clumsiness.

In Eclipse, when I call Assert.assertEquals(obj1,obj2) and that fails, how do I get the IDE to show me obj1 and obj2?

I'm using JExample, but I guess that shouldn't make a difference.


Edit: Here's what I see:

(source: yfrog.com)
.

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

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

发布评论

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

评论(6

開玄 2024-08-25 19:51:16
  1. 当您的对象有点复杂时,与故障跟踪进行比较并不是一件容易的任务。
  2. 如果您尚未重新定义 toString(),则与调试器进行比较很有用。作为解决方案,它仍然非常乏味,因为您应该用眼睛从两侧检查每个物体。

Junit Eclipse 插件在出现故障时提供了一个选项:“将实际结果与预期测试结果进行比较”。该视图与经典内容比较工具足够接近:
与字符串对象

问题是,只有当您使用 String 对象编写 assertEquals() 时,它才可用(在屏幕截图中,我们可以看到角落里的选项没有建议没有 <代码>字符串类):
与比较没有 String 对象

您可以在断言中对对象使用 toString() 但这不是一个好的解决方案:

  • 首先,您将 toString() 与 < code>equals(Object)...对一个的修改必须对另一个进行修改。
  • 其次,语义不再受到尊重。 toString() 应该返回一个有用的方法来调试一个对象的状态,而不是识别 Java 语义中的对象 (equals(Object))。

据我所知,我认为 JUnit Eclipse 插件缺少一个功能。
当比较失败时,即使我们比较的不是 String 对象,它也应该提供依赖于 toString() 方法的两个对象的比较。
它可以提供一种比较两个不相等对象的最小视觉方式。
当然,由于 equals(Object) 不一定与 toString() 相关,突出显示的差异应该用我们的眼睛来研究,但无论如何它已经是一个很好的基础了,比没有比较工具要好得多。

  1. Comparison with failure trace is not a easy task when your object is a little bit complex.
  2. Comparison with debugger is useful if you have not redefined toString(). It remains still very tedious as solution because you should inspect with your eyes each objects from both sides.

Junit Eclipse plugin offers a option when there is a failure : "Compare actual With Expected TestResult". The view is close enough to classic content comparison tools :
Comparison with String objects

Problem is that it is avaiable only when you writeassertEquals() with String objects (in the screenshot, we can see that the option in the corner is not proposed with no String class) :
Comparison with no String objects

You may use toString() on your object in assertion but it's not a good solution :

  • firstly, you correlate toString() with equals(Object)... modification of one must entail modification of the other.
  • secondly, the semantic is not any longer respected. toString() should return a useful method to debug the state of one object, not to identify an object in the Java semantic (equals(Object)).

According to me, I think that the JUnit Eclipse plugin misses a feature.
When comparison fails, even when we compare not String objects, it should offer a comparison of the two objects which rely on their toString() method.
It could offer a minimal visual way of comparing two unequals objects.
Of course, as equals(Object) is not necessarily correlated to toString(), highlighted differences should be studied with our eyes but it would be already a very good basis and anyway, it is much better than no comparison tool.

许你一世情深 2024-08-25 19:51:16

如果 JUnit 视图中的信息对您来说不够,您可以随时设置异常断点,例如 java.lang.AssertionError。运行测试时,调试器将在实际抛出异常之前立即停止。

If the information in the JUnit view is not enough for you, you can always set a exception breakpoint on, for example, java.lang.AssertionError. When running the test, the debugger will stop immediately before the exception is actually being thrown.

紫罗兰の梦幻 2024-08-25 19:51:16

Assert.assertEquals() 会将预期对象和实际对象的 toString() 表示形式放入它抛出的 AssertionFailedError 消息中,并且 eclipse将在 JUnit 视图的“失败跟踪”部分显示:

alt 文本
(来源:ibm.com

如果您想要检查复杂的对象,则必须使用调试器并在 Assert.assertEquals() 中放置一个断点

Assert.assertEquals() will put the toString() representation of the expected and actual object in the message of the AssertionFailedError it throws, and eclipse will display that in the "failure trace" part of the JUnit view:

alt text
(source: ibm.com)

If you have complex objects you want to inspect, you'll have to use the debugger and put a breakpoint inside Assert.assertEquals()

你不是我要的菜∠ 2024-08-25 19:51:16

你看到什么了?

当你执行assertTrue()并且失败时,你会看到一个null。

但是当你执行assertEquals时,它应该向你展示它的预期和实际得到的结果。

如果您正在使用 JUnit,请确保您正在查看 JUnit 视图并将鼠标移至失败的测试。

What are you seeing?

When you do assertTrue() and it fails, you see a null.

But when you do assertEquals, it is supposed to show you what it expected and what it actually got.

If you are using JUnit, mke sure you are looking at the JUnit view and moving the mouse to the failed test.

情绪 2024-08-25 19:51:16

即使您比较的对象不是字符串,FEST Assert 也会在断言失败时显示比较对话框。我在我的博客上更详细地解释了它

FEST Assert will display comparison dialog in case of assertion failure even when objects you compare are not strings. I explained it in more detail on my blog.

此刻的回忆 2024-08-25 19:51:16

如果您要比较的是字符串,那么您可以双击堆栈元素,它将弹出一个对话框,显示 Eclipse 中的差异。

但这仅适用于字符串。对于一般情况,查看真正原因的唯一方法是安装断点并单步执行。

If what you are comparing is a String then you can double click stack element and it will popup a dialog showing the diff in eclipse.

This only works with Strings though. For the general case the only way to see the real reason is to install a breakpoint and step into it.

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