的assertEquals 2列出忽略秩序 [英] AssertEquals 2 Lists ignore order
问题描述
这应该是很简单的问题,我相信。但不知何故,我无法在谷歌找到答案。
That should be really simple question I believe. But somehow I can't find answer in Google.
假设我有2列出字符串。第一个包含字符串A和字符串B,第二个包含字符串B和字符串A(注意,为了区别)。我想测试他们的的JUnit 以检查它们是否包含完全相同字符串。
Assume that I have 2 Lists of Strings. First contains "String A" and "String B", second one contains "String B" and "String A" (notice difference in order). I want to test them with JUnit to check whether they contains exactly the same Strings.
有没有检查无视为了弦乐平等的断言?对于给定的例子org.junit.Assert.assertEquals抛出的AssertionError
Is there any assert that checks equality of Strings that ignore order? For given example org.junit.Assert.assertEquals throws AssertionError
java.lang.AssertionError: expected:<[String A, String B]> but was:<[String B, String A]>
周围的工作是先列出排序,然后将它们传递给断言。但我想我的code是简单而干净越好。
Work around is to sort Lists firstly and then pass them to assertion. But I want my code to be as simple and clean as possible.
我使用 Hamcrest 1.3 的JUnit 4.11 1.9.5的Mockito
推荐答案
您可以使用<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/List.html#containsAll%28java.util.Collection%29\">List.containsAll检查第一个列表包含了从第二个,反之亦然每一个元素。
You can use List.containsAll to check that the first list contains every element from the second one, and vice versa.
assertTrue(first.containsAll(second) && second.containsAll(first));
这篇关于的assertEquals 2列出忽略秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!