为什么assertEquals(Object,Object)不使用junit ComparisonFailure? [英] Why junit ComparisonFailure is not used by assertEquals(Object, Object)?

查看:116
本文介绍了为什么assertEquals(Object,Object)不使用junit ComparisonFailure?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Junit 4中,您是否发现assertEquals(Object,Object)失败时抛出ComparisonFailure而不是AssertionError的任何缺点吗?

In Junit 4, do you see any drawback to throw a ComparisonFailure instead of an AssertionError when assertEquals(Object, Object) fails ?

assertEquals(对象,对象)抛出

assertEquals(Object, Object) throws

  • ComparisonFailure(如果期望值和实际值均为字符串)
  • AssertionError(如果不是一个字符串)
  • a ComparisonFailure if both expected and actual are String
  • an AssertionError if either is not a String

AssertionError消息已采用以下格式

"expected:<"+ expected.toString() +"> but was <"+ actual.toString()

(通过String.valueOf,请参见下面的由Assert.assertEquals(Object,Object)调用以构建AssertionError消息的junit-4.8.2方法):

(via String.valueOf, see below junit-4.8.2 method invoked by Assert.assertEquals(Object, Object) to build AssertionError message):

static String format(Object expected, Object actual) {
    ...
    String expectedString= String.valueOf(expected);
    String actualString=   String.valueOf(actual);
    ...
    return formatted+"expected:<"+ expectedString +"> but was:<"+ actualString +">";

ComparisonFailure提供了更具可读性的方法来发现eclipse对话框或

ComparisonFailure provide far more readable way to spot the differences in dialog box of eclipse or Intellij IDEA (FEST-Assert throws this exception)

[更新:已对问题进行了编辑,使之重点放在"ComparisonFailure/AssertionError"讨论上.]

[Update: question edited to focus on ComparisonFailure/AssertionError discussion.]

推荐答案

我们从比较字符串开始,因为很明显如何使错误消息更有用.我们从来没有将CompareFailure扩展到通用对象,因为尚不清楚如何以通用方式进行操作.正如其他人所建议的那样,如果可以提供更好的错误消息或转到Hamcrest,则欢迎添加特殊的断言,后者提供了添加有用的故障消息的通用机制.

We started with comparing strings because it was obvious how to make the error message more helpful. We never expanded ComparisonFailure to general objects because it wasn't clear how to do so in a general way. As other have suggested, you're welcome to add special assertions if you can provide better error messages or move to Hamcrest, which provides a general mechanism for adding useful failure messages.

此致

肯特

这篇关于为什么assertEquals(Object,Object)不使用junit ComparisonFailure?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆