JUnit 4 比较集
您如何简洁地断言 Collection
元素(特别是 JUnit 4 中的 Set
)的相等性?
How would you succinctly assert the equality of Collection
elements, specifically a Set
in JUnit 4?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以断言两个
Set
彼此相等,这会调用Set
equals()
方法。如果两个
Set
大小相同且包含相同元素,则此@Test
将通过。You can assert that the two
Set
s are equal to one another, which invokes theSet
equals()
method.This
@Test
will pass if the twoSet
s are the same size and contain the same elements.Apache Commons 再次拯救了我们。
就像魅力一样。我不知道为什么,但我发现对于集合,以下
assertEquals(coll1, coll2)
并不总是有效。如果我失败了,我有两个由 Sets 支持的集合。 hamcrest 和 junit 都不会说这些集合是平等的,尽管我确信它们是平等的。使用 CollectionUtils 它可以完美地工作。Apache commons to the rescue again.
Works like a charm. I don't know why but I found that with collections the following
assertEquals(coll1, coll2)
doesn't always work. In the case where it failed for me I had two collections backed by Sets. Neither hamcrest nor junit would say the collections were equal even though I knew for sure that they were. Using CollectionUtils it works perfectly.with hamcrest:
使用普通断言:
注意:t 的 equals() 方法使用具体集合类
with hamcrest:
with plain assert:
NB:t the equals() method of the concrete set class is used
一个特别有趣的情况是,当您比较
和
时,到目前为止,我看到的唯一解决方案是将它们都更改为集合
,或者我可以逐个元素地比较它们。
A particularly interesting case is when you compare
and
So far, the only solution I see is to change both of them into sets
Or I could compare them element by element.
作为基于数组的附加方法......您可以考虑在 junitx 中使用无序数组断言。虽然 Apache CollectionUtils 示例可以工作,但那里也有一个可靠的断言扩展包:
我认为该
方法对您来说将更具可读性和可调试性(所有集合都支持 toArray(),因此它应该足够容易使用 当然,
这里的缺点是,junitx 是一个额外的 jar 文件或 Maven 条目...
As an additional method that is array based ... you can consider using unordered array assertions in junitx . Although the Apache CollectionUtils example will work, there is a pacakge of solid assertion extensions there as well :
I think that the
approach will be much more readable and debuggable for you (all Collections support toArray(), so it should be easy enough to use the ArrayAssert methods.
Of course the downside here is that, junitx is an additional jar file or maven entry...
我喜欢 Hans-Peter Störr 的解决方案......但我认为它不太正确。遗憾的是
containsInAnyOrder
不接受要比较的对象Collection
。所以它必须是Matcher
的Collection
:导入是:
I like the solution of Hans-Peter Störr... But I think it is not quite correct. Sadly
containsInAnyOrder
does not accept aCollection
of objetcs to compare to. So it has to be aCollection
ofMatcher
s:The import are:
检查 这篇文章。其中的一个例子:
Check this article. One example from there:
使用 Hamcrest:
当集合具有不同的数据类型时,这也适用,并报告差异而不是仅仅失败。
Using Hamcrest:
This works also when the sets have different datatypes, and reports on the difference instead of just failing.
你可以这样做 ->
assertThat(actualSet).containsExactlyInAnyOrder(expectedSet);
You can do it like that ->
assertThat(actualSet).containsExactlyInAnyOrder(expectedSet);
如果您想检查 List 或 Set 是否包含一组特定值(而不是将其与已存在的集合进行比较),通常集合的 toString 方法很方便:
这比首先构造预期集合并比较要短一些它与实际集合相结合,并且更容易编写和纠正。
(诚然,这不是一个特别干净的方法,并且无法区分元素“foo,bar”和两个元素“foo”和“bar”。但在实践中我认为最重要的是编写测试既简单又快速,否则许多开发人员在没有压力的情况下就不会这样做。)
If you want to check whether a List or Set contains a set of specific values (instead of comparing it with an already existing collection), often the toString method of collections is handy:
This is a bit shorter than first constructing the expected collection and comparing it with the actual collection, and easier to write and correct.
(Admittedly, this is not a particularily clean method, and can't distinguish an element "foo, bar" from two elements "foo" and "bar". But in practice I think it's most important that it's easy and fast to write tests, otherwise many developers just won't without being pressed.)
containsInAnyOrder() - 帮助我。
containsInAnyOrder() - help me.