java 断言列表时 assertEquals 到底检查什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16788275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:00:57  来源:igfitidea点击:

What exactly does assertEquals check for when asserting lists?

javajunitmockito

提问by liloka

In my test I'm asserting that the list I return is an alphabetically ordered list of the one I just created.

在我的测试中,我断言我返回的列表是我刚刚创建的列表的按字母顺序排列的列表。

What exactly does the assertEquals do check for? Does it check the ordering of the list or just its contents?

assertEquals 究竟会检查什么?它是检查列表的顺序还是仅检查其内容?

So if I have a list of { "Fred", "Bob", "Anna" } would list 2 of { "Anna", "Bob", "Fred" } return true as they contain the same object, regardless of order?

因此,如果我有一个 { "Fred", "Bob", "Anna" } 列表,会列出 { "Anna", "Bob", "Fred" } 中的 2 个,因为它们包含相同的对象,无论顺序如何?

回答by Kevin Bowersox

If you follow the source code of jUnit. You will see that assertEqualseventually calls the equalsmethod on the objects provided in the the isEqualsmethod.

如果您遵循 jUnit 的源代码。您将看到assertEquals最终在equals方法中提供的对象上调用该isEquals方法。

private static boolean isEquals(Object expected, Object actual) {
    return expected.equals(actual);
}

Source Code:https://github.com/junit-team/junit/blob/master/src/main/java/org/junit/Assert.java

源代码:https : //github.com/junit-team/junit/blob/master/src/main/java/org/junit/Assert.java

This will call the .equals()method on the implementation of List. Here is the source codefor the .equals()implementation of `ArrayList'.

这将调用.equals()的实现方法List。下面是源代码.equals()'ArrayList的”的实施。

ArrayList.equals()

ArrayList.equals()

  public boolean equals(Object o) {
      if (o == this) //Equality check
          return true;
      if (!(o instanceof List))  //Type check
          return false;
      ListIterator<E> e1 = listIterator();
      ListIterator e2 = ((List) o).listIterator();
      while(e1.hasNext() && e2.hasNext()) {
          E o1 = e1.next();
          Object o2 = e2.next();
          if (!(o1==null ? o2==null : o1.equals(o2))) //equality check of list contents
              return false;
      }
      return !(e1.hasNext() || e2.hasNext());
  }

回答by sanbhat

There is no special assertEqualsfunctions in junit.framework.Assertwhich accepts Listor Collectionobject.

没有接受或对象的特殊assertEquals函数。junit.framework.AssertListCollection

When you say assertEquals(list1, list2);it simply checks whether list1.equals(list2)and if notthrows AssertionError

当你说它assertEquals(list1, list2);只是检查是否list1.equals(list2)如果不抛出AssertionError

回答by Djon

A List is by definition ordered, so I would say it calls equals() and check the elements of both lists one by one.

List 根据定义是有序的,所以我会说它调用 equals() 并一一检查两个列表的元素。

Ok let me rephrase, what do you mean by "the ordering of the list" and "its content"?

好的,让我改写一下,“列表的顺序”和“其内容”是什么意思?

If the list you create is [b,a], the ordered one will be [a,b]. [a,b] can only be equal to [a,b] because a List is ordered.

如果您创建的列表是 [b,a],则有序列表将是 [a,b]。[a,b] 只能等于 [a,b] 因为 List 是有序的。

Two sets, [a,b] and [b,a] are not ordered but equal.

两个集合 [a,b] 和 [b,a] 无序但相等。

Plus if you look at the source, it DOES call equals(), so why the downvotes?

另外,如果您查看源代码,它确实调用了 equals(),那么为什么不投票呢?